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.MaDevRm;
import com.bonus.material.device.mapper.BmFileInfoMapper;
import com.bonus.material.device.mapper.MaDevRmMapper;
import com.bonus.material.device.service.MaDevRmService;
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;

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

    @Resource
    private MaDevRmMapper maDevRmMapper;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    /**
     * 装备质检列表
     *
     * @param maDevRm
     * @return
     */
    @Override
    public List<MaDevRm> selectDevQcList(MaDevRm maDevRm) {
        maDevRm.setRmCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId().toString());
        return maDevRmMapper.selectDevQcList(maDevRm);
    }

    /**
     * 新增装备质检
     *
     * @param maDevRm
     * @return
     */
    @Override
    public AjaxResult insertDevQc(MaDevRm maDevRm) {
        if (maDevRm == null) {
            return AjaxResult.error(HttpCodeEnum.TO_PARAM_NULL.getCode(), HttpCodeEnum.TO_PARAM_NULL.getMsg());
        }
        String code = getString();
        maDevRm.setRmCode(code);
        maDevRm.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        maDevRm.setCreateTime(DateUtils.getNowDate());
        maDevRm.setRmCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId().toString());
        int result = maDevRmMapper.insertDevQc(maDevRm);
//        devInfoMapper.updateDevInfoIsQc(maDevRm);
        if (result > 0 && maDevRm.getId() != null) {
            if (CollectionUtils.isNotEmpty(maDevRm.getFileInfoList())) {
                for (BmFileInfo bmFileInfo : maDevRm.getFileInfoList()) {
                    bmFileInfo.setTaskId(Long.valueOf(maDevRm.getId()));
                    bmFileInfo.setModelId(Long.valueOf(maDevRm.getMaId()));
                    bmFileInfo.setTaskType(MaterialConstants.MATERIAL_FILE_TYPE_CODE);
                    // 维保附件
                    bmFileInfo.setFileType(6L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                    maDevRmMapper.updateFileId(maDevRm.getId(), bmFileInfo.getId().intValue());
                }
            }
        }
        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 = maDevRmMapper.selectTaskNumByMonth(nowDate);
        if (StringUtils.isNotBlank(taskNum)) {
            // 将字符串转换为整数
            int num = Integer.parseInt(taskNum);
            // 执行加一操作
            num++;
            // 将结果转换回字符串格式，并确保结果是4位数，不足4位则在前面补0
            taskNum = String.format("%04d", num);
        } else {
            taskNum = "0001";
        }
        return "WXBY" + format + "-" + taskNum;
    }

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

    @Override
    public List<MaDevRm> selectQcList(MaDevRm maDevRm) {
        List<MaDevRm> maDevRms = maDevRmMapper.selectQcList(maDevRm);
        for (MaDevRm devRm : maDevRms) {
            String url = bmFileInfoMapper.getUrlById(devRm.getFileId());
            devRm.setUrl(url);
        }
        return maDevRms;
    }
}
