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.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.device.domain.SafeBookInfo;
import com.bonus.material.device.mapper.BmFileInfoMapper;
import com.bonus.material.device.mapper.DevInfoMapper;
import com.bonus.material.device.mapper.SafeBookMapper;
import com.bonus.material.device.service.SafeBookService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2024/12/4 - 16:37
 */
@Service
public class SafeBookServiceImpl implements SafeBookService {

    @Resource
    private SafeBookMapper safeBookMapper;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    @Resource
    private DevInfoMapper devInfoMapper;

    @Override
    public List<SafeBookInfo> getSafeBookByMaId(SafeBookInfo safeBookInfo) {
        safeBookInfo.setUploadCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId().intValue());
        List<SafeBookInfo> safeBookByMaId = safeBookMapper.getSafeBookByMaId(safeBookInfo);
        for (SafeBookInfo bookInfo : safeBookByMaId) {
            List<BmFileInfo> files = bmFileInfoMapper.getInfoByMaId(bookInfo.getMaId());
            bookInfo.setFileInfoList(files);
        }
        return safeBookByMaId;
    }

    @Override
    public Integer addSafeBook(SafeBookInfo safeBookInfo) {
        safeBookInfo.setCode(getString());
        safeBookInfo.setUploadPerson(SecurityUtils.getLoginUser().getUserid());
        safeBookInfo.setUploadCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId().intValue());
        Integer i = safeBookMapper.addSafeBook(safeBookInfo);
        devInfoMapper.updateDevInfoIsSafeBook(safeBookInfo);
        if (i > 0) {
            if (CollectionUtils.isNotEmpty(safeBookInfo.getFileInfoList())) {
                for (BmFileInfo bmFileInfo : safeBookInfo.getFileInfoList()) {
                    bmFileInfo.setModelId(Long.valueOf(safeBookInfo.getMaId()));
                    bmFileInfo.setTaskType(MaterialConstants.MATERIAL_FILE_TYPE_CODE);
                    // 安全证书附件
                    bmFileInfo.setFileType(5L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                }
            }
        }
        return i;
    }

    @Override
    public Integer delSafeBook(SafeBookInfo safeBookInfo) {
        Integer i = safeBookMapper.delSafeBook(safeBookInfo);
        if (i > 0) {
            bmFileInfoMapper.deleteBmFileInfoByMaId(safeBookInfo.getMaId(), 5);
        }
        return i;
    }

    @Override
    public Integer editSafeBook(SafeBookInfo safeBookInfo) {
        safeBookMapper.updateTime(safeBookInfo.getMaId());
        Integer i = bmFileInfoMapper.deleteBmFileInfoByMaId(safeBookInfo.getMaId(), 5);
        if (i > 0) {
            if (CollectionUtils.isNotEmpty(safeBookInfo.getFileInfoList())) {
                for (BmFileInfo bmFileInfo : safeBookInfo.getFileInfoList()) {
                    bmFileInfo.setModelId(Long.valueOf(safeBookInfo.getMaId()));
                    bmFileInfo.setTaskType(MaterialConstants.MATERIAL_FILE_TYPE_CODE);
                    // 安全证书附件
                    bmFileInfo.setFileType(5L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                }
            }
        }
        return i;
    }

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

}
