package com.casic.missiles.service.Impl.equipment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.base.service.IBaseExportService;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.equipment.CheckInfoRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.enums.PrefixCodeEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.equipment.EquipmentCheckInfoFileMapper;
import com.casic.missiles.mapper.equipment.EquipmentCheckInfoMapper;
import com.casic.missiles.model.equipment.EquipmentCheckInfo;
import com.casic.missiles.model.equipment.EquipmentCheckInfoFile;
import com.casic.missiles.modular.system.dao.UserMapper;
import com.casic.missiles.service.equipment.IEquipmentCheckInfoFileService;
import com.casic.missiles.service.equipment.IEquipmentCheckInfoService;
import com.casic.missiles.utils.DictCodeUtils;
import com.casic.missiles.utils.NumberGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Service("IEquipmentCheckInfoService")
public class EquipmentCheckInfoServiceImpl extends ServiceImpl<EquipmentCheckInfoMapper, EquipmentCheckInfo> implements IEquipmentCheckInfoService {

    @Resource
    private EquipmentCheckInfoMapper equipmentCheckInfoMapper;

    @Resource
    private IEquipmentCheckInfoService checkInfoService;

    @Resource
    private IBaseExportService iBaseExportService;

    @Resource
    private EquipmentCheckInfoFileMapper equipmentCheckInfoFileMapper;

    @Resource
    private IEquipmentCheckInfoFileService checkInfoFileService;

    @Resource
    private UserMapper userMapper;

    @Override
    public Page<EquipmentCheckInfo> checkInfoList(Page<EquipmentCheckInfo> page, CheckInfoRequest request) throws Exception {
        QueryWrapper<EquipmentCheckInfo> wrapper = getWrapper(request);
        Page<EquipmentCheckInfo> resultPage = equipmentCheckInfoMapper.selectPage(page, wrapper);
        for (EquipmentCheckInfo equipmentCheckInfo : resultPage.getRecords()) {
            DictCodeUtils.convertDictCodeToName(equipmentCheckInfo);
        }
        return resultPage;
    }

    @Override
    public ReturnDTO deleteCheckInfo(IdDTO idDto) {
        if(equipmentCheckInfoMapper.deleteById(idDto.getId())>0){
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @Override
    public EquipmentCheckInfo selectCheckInfo(IdDTO idDto) throws Exception {
        EquipmentCheckInfo equipmentCheckInfo = equipmentCheckInfoMapper.selectById(idDto.getId());
        DictCodeUtils.convertDictCodeToName(equipmentCheckInfo);
        QueryWrapper<EquipmentCheckInfoFile> wrapper = new QueryWrapper<>();
        wrapper.eq("check_id",idDto.getId());
        equipmentCheckInfo.setCreateUserName(userMapper.selectById(equipmentCheckInfo.getCreateUser()).getName());
        List<EquipmentCheckInfoFile> checkInfoFiles = equipmentCheckInfoFileMapper.selectList(wrapper);
        equipmentCheckInfo.setFileList(checkInfoFiles);
        return equipmentCheckInfo;
    }

    @Override
    @Transactional
    public ReturnDTO addCheckInfo(EquipmentCheckInfo equipmentCheckInfo) {
        long maxNo = equipmentCheckInfoMapper.selectMaxNo();
        String checkNo = NumberGeneratorUtil.getContactNo(PrefixCodeEnum.CHECK_PREFIX, maxNo);
        equipmentCheckInfo.setCheckNo(checkNo);
        if(equipmentCheckInfoMapper.insert(equipmentCheckInfo) > 0){
            Long id = equipmentCheckInfo.getId();
            for (EquipmentCheckInfoFile file : equipmentCheckInfo.getFileList()) {
                file.setCheckId(id);
            }
            boolean saveBatchFlg = checkInfoFileService.saveBatch(equipmentCheckInfo.getFileList());
            if(saveBatchFlg){
                return ReturnUtil.success();
            }
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @Override
    public void exportCheckInfoList(CheckInfoRequest request, HttpServletResponse response) throws Exception {
        List<EquipmentCheckInfo> resultList;
        if(!CollectionUtils.isEmpty(request.getIds())){
            resultList = checkInfoService.listByIds(request.getIds());
        }else{
            QueryWrapper<EquipmentCheckInfo> wrapper = getWrapper(request);
            resultList = equipmentCheckInfoMapper.selectList(wrapper);
        }
        for (EquipmentCheckInfo equipmentCheckInfo : resultList) {
            DictCodeUtils.convertDictCodeToName(equipmentCheckInfo);
        }
        iBaseExportService.exportExcel(response, EquipmentCheckInfo.class,resultList, ExportEnum.CHECK_INFO.getSheetName());
    }

    private QueryWrapper<EquipmentCheckInfo> getWrapper(CheckInfoRequest request) {
        QueryWrapper<EquipmentCheckInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(request.getCheckNo()), "check_no", request.getCheckNo());
        wrapper.like(StringUtils.isNotBlank(request.getCheckName()), "check_name", request.getCheckName());
        wrapper.apply(StringUtils.isNotBlank(request.getCheckStartDate()),
                "{0} >= DATE_FORMAT(check_date,'%Y-%m-%d')", request.getCheckStartDate());
        wrapper.apply(StringUtils.isNotBlank(request.getCheckEndDate()),
                "{0} <= DATE_FORMAT(check_date,'%Y-%m-%d')", request.getCheckEndDate());
        wrapper.eq("is_del", 0);
        wrapper.orderByDesc("create_time");
        return wrapper;
    }
}
