package com.bonus.material.device.service.impl;

import com.bonus.common.biz.constant.MaterialConstants;
import com.bonus.common.biz.domain.BmFileInfo;
import com.bonus.common.biz.enums.HttpCodeEnum;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.device.domain.MaDevQc;
import com.bonus.material.device.mapper.BmFileInfoMapper;
import com.bonus.material.device.mapper.DevInfoMapper;
import com.bonus.material.device.mapper.MaDevQcMapper;
import com.bonus.material.device.service.MaDevQcService;
import com.bonus.system.api.domain.SysUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import static com.bonus.common.biz.constant.MaterialConstants.ADMIN_ID;

/**
 * @Author ma_sh
 * @create 2024/12/5 15:53
 */
@Service
public class MaDevQcServiceImpl implements MaDevQcService {

    @Resource
    private MaDevQcMapper maDevQcMapper;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    @Resource
    private DevInfoMapper devInfoMapper;

    /**
     * 装备质检列表
     *
     * @param maDevQc
     * @return
     */
    @Override
    public List<MaDevQc> selectDevQcList(MaDevQc maDevQc) {
        maDevQc.setQcCom(SecurityUtils.getLoginUser().getSysUser().getDeptId().toString());
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        if(ADMIN_ID.equals(userId)){
            maDevQc.setCompanyId(1L);
        }else{
            maDevQc.setCompanyId(deptId);
        }
        return maDevQcMapper.selectDevQcList(maDevQc);
    }

    /**
     * 新增装备质检
     *
     * @param maDevQc
     * @return
     */
    @Override
    public AjaxResult insertDevQc(MaDevQc maDevQc) {
        if (maDevQc == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        String code = getString();
        maDevQc.setQcCode(code);
        maDevQc.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        maDevQc.setCreateTime(DateUtils.getNowDate());
        maDevQc.setQcCom(Optional.ofNullable(SecurityUtils.getLoginUser().getSysUser().getCompanyId()).orElse(SecurityUtils.getLoginUser().getSysUser().getDeptId()).toString());
        int result = maDevQcMapper.insertDevQc(maDevQc);
        devInfoMapper.updateDevInfoIsQc(maDevQc);
        if (result > 0 && maDevQc.getId() != null) {
            if (CollectionUtils.isNotEmpty(maDevQc.getFileInfoList())) {
                for (BmFileInfo bmFileInfo : maDevQc.getFileInfoList()) {
                    bmFileInfo.setTaskId(Long.valueOf(maDevQc.getId()));
                    bmFileInfo.setModelId(Long.valueOf(maDevQc.getMaId()));
                    bmFileInfo.setTaskType(MaterialConstants.MATERIAL_FILE_TYPE_CODE);
                    // 质检附件
                    bmFileInfo.setFileType(4L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                    maDevQcMapper.updateFileId(maDevQc.getId(), bmFileInfo.getId().intValue());
                }
            }
        }
        return result > 0 ? AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg()) : AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    @Override
    public AjaxResult addDev(MaDevQc maDevQc) {
        if (maDevQc == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        String code = getString();
        maDevQc.setQcCode(code);
        maDevQc.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        maDevQc.setCreateTime(DateUtils.getNowDate());
        maDevQc.setQcCom(Optional.ofNullable(SecurityUtils.getLoginUser().getSysUser().getCompanyId()).orElse(SecurityUtils.getLoginUser().getSysUser().getDeptId()).toString());
        int result = maDevQcMapper.insertDevQc(maDevQc);
        devInfoMapper.updateDevInfoIsQc(maDevQc);
        if (result > 0 && maDevQc.getId() != null) {
            if (CollectionUtils.isNotEmpty(maDevQc.getFileInfoList())) {
                for (BmFileInfo bmFileInfo : maDevQc.getFileInfoList()) {
                    bmFileInfo.setTaskId(Long.valueOf(maDevQc.getId()));
                    bmFileInfo.setModelId(Long.valueOf(maDevQc.getMaId()));
                    bmFileInfo.setTaskType(MaterialConstants.MATERIAL_FILE_TYPE_CODE);
                    // 质检附件
                    bmFileInfo.setFileType(4L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                }
            }
        }
        return result > 0 ? AjaxResult.success(HttpCodeEnum.SUCCESS.getMsg()) : AjaxResult.error(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 生成需求编号
     *
     * @return
     */
    private String getString() {
        //根据前台传过来的数据，生成需求编号
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date nowDate = DateUtils.getNowDate();
        String format = dateFormat.format(nowDate);
        String taskNum = maDevQcMapper.selectTaskNumByMonth(nowDate);
        if (StringUtils.isNotBlank(taskNum)) {
            // 将字符串转换为整数
            int num = Integer.parseInt(taskNum);
            // 执行加一操作
            num++;
            // 将结果转换回字符串格式，并确保结果是4位数，不足4位则在前面补0
            taskNum = String.format("%04d", num);
        } else {
            taskNum = "0001";
        }
        return "ZJ" + format + "-" + taskNum;
    }

    /**
     * 删除装备质检
     *
     * @param maDevQc
     * @return
     */
    @Override
    public Integer deleteById(MaDevQc maDevQc) {
        int i = maDevQcMapper.deleteDevQcByFileId(maDevQc.getFileId());
        if (i > 0) {
            bmFileInfoMapper.deleteBmFileInfoByFileId(maDevQc.getFileId(), 4);
        }
        return i;
    }

    @Override
    public List<MaDevQc> selectQcList(MaDevQc maDevQc) {
        return maDevQcMapper.selectQcList(maDevQc);
    }

    @Override
    public Integer selectDevQcListByMaId(Integer maId) {
        return maDevQcMapper.selectDevQcListByMaId(maId);
    }
}
