package com.bonus.material.contract.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.core.utils.StringUtils;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.contract.domain.BmContract;
import com.bonus.material.contract.mapper.BmContractMapper;
import com.bonus.material.contract.service.BmContractService;
import com.bonus.material.device.mapper.BmFileInfoMapper;
import org.springframework.stereotype.Service;

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

/**
 * @Author：liang.chao
 * @Date：2024/12/13 - 9:16
 */
@Service
public class BmContractServiceImpl implements BmContractService {
    @Resource
    private BmContractMapper bmContractMapper;

    @Resource
    private BmFileInfoMapper bmFileInfoMapper;

    @Override
    public List<BmContract> list(BmContract bmContract) {
        bmContract.setOwnerCom(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        List<BmContract> list = bmContractMapper.list(bmContract);
        for (BmContract contract : list) {
            BmFileInfo bmFileInfo = new BmFileInfo();
            bmFileInfo.setModelId(Long.valueOf(contract.getId())).setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT).setFileType(0L);
            List<BmFileInfo> bmFileInfos = bmFileInfoMapper.selectBmFileInfoList(bmFileInfo);
            contract.setBmFileInfoList(bmFileInfos);
        }
        return list;
    }

    @Override
    public Integer add(BmContract bmContract) throws UnsupportedEncodingException {
        bmContract.setContractCode(getString());
        bmContract.setOwnerId(SecurityUtils.getLoginUser().getUserid());
        bmContract.setOwnerCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
        bmContract.setStatus(0);
        Integer add = bmContractMapper.add(bmContract);
        if (add > 0) {
            if (bmContract.getBmFileInfoList() != null && bmContract.getBmFileInfoList().size() > 0) {
                for (BmFileInfo bmFileInfo : bmContract.getBmFileInfoList()) {
                    bmFileInfo.setModelId(Long.valueOf(bmContract.getId()));
                    bmFileInfo.setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT);
                    // 合同照片附件
                    bmFileInfo.setFileType(0L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                }
            }
        }
        return add;
    }

    @Override
    public Integer edit(BmContract bmContract) throws UnsupportedEncodingException {
        bmContract.setOwnerId(SecurityUtils.getLoginUser().getUserid());
        bmContract.setOwnerCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
        Integer edit = bmContractMapper.edit(bmContract);
        if (edit > 0) {
            if (bmContract.getBmFileInfoList() != null && bmContract.getBmFileInfoList().size() > 0) {
                BmFileInfo fileInfo = new BmFileInfo();
                fileInfo.setModelId(Long.valueOf(bmContract.getId())).setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT).setFileType(0L);
                bmFileInfoMapper.deleteBmFileInfo(fileInfo);
                for (BmFileInfo bmFileInfo : bmContract.getBmFileInfoList()) {
                    bmFileInfo.setModelId(Long.valueOf(bmContract.getId()));
                    bmFileInfo.setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT);
                    // 合同照片附件
                    bmFileInfo.setFileType(0L);
                    bmFileInfo.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString());
                    bmFileInfoMapper.insertBmFileInfo(bmFileInfo);
                }
            }
        }
        return edit;
    }

    @Override
    public Integer del(BmContract bmContract) {
        Integer del = bmContractMapper.del(bmContract);
        if (del > 0) {
            BmFileInfo fileInfo = new BmFileInfo();
            fileInfo.setModelId(Long.valueOf(bmContract.getId())).setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT).setFileType(0L);
            bmFileInfoMapper.deleteBmFileInfo(fileInfo);
        }
        return del;
    }

    @Override
    public Integer updateStatus(BmContract bmContract) {
        if (bmContract.getStatus() == 0) {
            // 禁用
            return bmContractMapper.updateStatus(bmContract);
        } else {
            // 开启
            Integer i = bmContractMapper.updateStatus(bmContract);
            if (i > 0) {
                // 其他的禁用
                bmContract.setOwnerCom(SecurityUtils.getLoginUser().getSysUser().getCompanyId());
                bmContractMapper.updateStatusOther(bmContract);
                return i;
            } else {
                return i;
            }
        }
    }

    @Override
    public List<BmContract> lisTemplate(BmContract bmContract) {
        return bmContractMapper.lisTemplate(bmContract);
    }

    @Override
    public BmContract disclaimer() {
        return bmContractMapper.disclaimer();
    }

    @Override
    public BmContract getContract(BmContract bmContract) {
        bmContract.setStatus(1);
        return bmContractMapper.getContract(bmContract);
    }

    @Override
    public List<BmContract> getContractUrl(BmContract bmContract) {
        List<BmContract> list = bmContractMapper.list(bmContract);
        for (BmContract contract : list) {
            BmFileInfo bmFileInfo = new BmFileInfo();
            bmFileInfo.setModelId(Long.valueOf(contract.getId())).setTaskType(MaterialConstants.APPENDICES_OF_CONTRACT).setFileType(0L);
            List<BmFileInfo> bmFileInfos = bmFileInfoMapper.selectBmFileInfoList(bmFileInfo);
            contract.setBmFileInfoList(bmFileInfos);
        }
        return list;
    }

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