package com.jwipc.sys.logic.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.MoldFormConvert;
import com.jwipc.sys.convert.MoldMoveConvert;
import com.jwipc.sys.dto.MoldFormDTO;
import com.jwipc.sys.dto.MoldMoveDTO;
import com.jwipc.sys.entity.MoldForm;
import com.jwipc.sys.entity.MoldMove;
import com.jwipc.sys.logic.MoldFormLogic;
import com.jwipc.sys.model.param.MoldFormParam;
import com.jwipc.sys.model.po.MoldFormPO;
import com.jwipc.sys.model.po.MoldMovePO;
import com.jwipc.sys.model.query.MoldFormQuery;
import com.jwipc.sys.model.query.MoldMoveQuery;
import com.jwipc.sys.service.IMoldFormService;
import com.jwipc.sys.service.IMoldMoveService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author AL
 * @date 2024/1/31 15:01
 * @desc 模具
 */
@Service
@RequiredArgsConstructor
public class MoldFormLogicImpl implements MoldFormLogic {

    private final IMoldFormService moldFormService;
    private final IMoldMoveService moldMoveService;
    private final MoldFormConvert moldFormConvert;
    private final MoldMoveConvert moldMoveConvert;

    @Override
    public PageView findByPage(MoldFormQuery query) {
        final Page<MoldForm> dbPage = PageUtil.toPage(query);

        List<MoldFormPO> moldFormPOS = moldFormService.findPageByQuery(dbPage, query);
//        if ((double) query.getActualUseTimes() / param.getUseTimes() >= 0.8) {
//            param.setIsCallPolice(1);
//        } else {
//            param.setIsCallPolice(0);
//        }

        return PageUtil.getPV(moldFormConvert.po2PageListDTO(moldFormPOS), dbPage.getTotal());
    }
    @Override
    public PageView moldMoveFindByPage(MoldMoveQuery query) {
        final Page<MoldMove> dbPage = PageUtil.toPage(query);
                Long moldId = query.getMoldId();
        List<MoldMovePO> moldMovePOS = moldMoveService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(moldMoveConvert.po2PageListDTO(moldMovePOS), dbPage.getTotal());
    }
//    @Override
//    public MoldFormDTO moldMoveFindByPage1(Long moldId) {
//       // final Page<MoldMove> dbPage = PageUtil.toPage(query);
//
//        List<MoldMovePO> moldMovePOS = moldMoveService.findPageByQuery(dbPage, query);
//
//        return PageUtil.getPV(moldMoveConvert.po2PageListDTO(moldMovePOS), dbPage.getTotal());
//    }
    @Override
    public MoldFormDTO getById(Long id) {
        if (id != null) {
            MoldFormPO moldFormPO = moldFormService.getDataById(id);
            return moldFormConvert.po2DTO(moldFormPO);
        }else{
            return new MoldFormDTO();
        }
    }
    @Override
    public MoldMoveDTO moldMoveGetById(Long id) {
        if (id != null) {
            MoldMovePO moldMovePO = moldMoveService.getDataById(id);
            return moldMoveConvert.po2DTO(moldMovePO);
        }else{
            return new MoldMoveDTO();
        }
    }

    /**
     模具附件上传
     */
    @Override
    public void saveOnFile(Long id, String fileIds) {
//        final LambdaQueryWrapper<QmAuditManage> eq =
//                new LambdaQueryWrapper<QmAuditManage>().eq(
//                        QmAuditManage::getAuditAttachment, auditAtt);
//        qmAuditManageService.remove(eq);
        if (StrUtil.isEmpty(fileIds)) {
            return;
        }
        MoldForm moldForm = moldFormService.getById(id);
        moldForm.setAttachment(fileIds);
        moldFormService.saveOrUpdate(moldForm);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void moldMoveSaveOrUpdate(MoldMoveDTO param) {
        if(StringUtils.isNotEmpty(param.getCreateTimeDesc())){
            param.setCreateTime(LocalDateTimeUtil.parse(param.getCreateTimeDesc(),"yyyy-MM-dd"));
        }
        //保存
//        Objects.nonNull();
//        Objects.isNull()
        if (Objects.isNull(param.getId())){

            final LambdaQueryWrapper<MoldMove> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldMove>().eq(
                    MoldMove::getId, param.getId()).last(" limit 1");
            param.setCreater(LoginUtil.getUserId());
            param.setCreateDate(LocalDateTime.now());
            param.setCreaterName(LoginUtil.getCurrentUser().getName());

        }else {
            final LambdaQueryWrapper<MoldMove> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldMove>().eq(
                    MoldMove::getId, param.getId()).ne(MoldMove::getId,param.getId()).last(" limit 1");
        }
        if(!Objects.isNull(param.getFlag()) && param.getFlag() == 2){
            param.setDeleteFlag(1);
            param.setDeleteNote(param.getDeleteNote());
        }
        moldMoveService.saveOrUpdate(moldMoveConvert.dto2Entity(param));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(MoldFormParam param) {
        //保存
//        Objects.nonNull();
//        Objects.isNull()
        if (Objects.isNull(param.getId())){

            final LambdaQueryWrapper<MoldForm> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldForm>().eq(
                    MoldForm::getMoldCode, param.getMoldCode()).last(" limit 1");
            if (ObjectUtil.isNotNull(moldFormService.getOne(resourcesLambdaQueryWrapper))){
                throw new DaoException("模具编码重复");
            }
            if (ObjectUtil.isNotNull(param.getActualUseTimes()) && (double) param.getActualUseTimes() / param.getUseTimes() >= 0.8) {
                param.setIsCallPolice(1);
            } else {
                param.setIsCallPolice(0);
            }
            if (!(param.getUseTimes() instanceof Integer) && !Objects.isNull(param.getUseTimes())) {
                throw new IllegalArgumentException("寿命次数只能为数字");
            }
            param.setCreateTime(LocalDateTime.now());
            param.setCreater(LoginUtil.getUserId());
            param.setCreaterName(LoginUtil.getName());


        }else {
            final LambdaQueryWrapper<MoldForm> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldForm>().eq(
                    MoldForm::getMoldCode, param.getMoldCode()).ne(MoldForm::getId,param.getId()).last(" limit 1");
            if (ObjectUtil.isNotNull(moldFormService.getOne(resourcesLambdaQueryWrapper))){
                throw new DaoException("模具编码重复");
            }
            //param.setLastUpdateTime(LocalDateTime.now());
            if (ObjectUtil.isNotNull(param.getActualUseTimes()) && (double) param.getActualUseTimes() / param.getUseTimes() >= 0.8) {
                param.setIsCallPolice(1);
            } else {
                param.setIsCallPolice(0);
            }
            if (!(param.getUseTimes() instanceof Integer) && !Objects.isNull(param.getUseTimes())) {
                throw new IllegalArgumentException("寿命次数只能为数字");
            }
        }
        if(param.getFlag() == 2){
            param.setDeleteFlag(1);
            param.setUseState(3);
        }
        moldFormService.saveOrUpdate(moldFormConvert.param2Entity(param));
    }
    @Override
    public void moldMoveRemove(MoldMoveDTO param) {
        MoldMove moldMove = moldMoveService.getOne(new LambdaQueryWrapper<MoldMove>().eq(MoldMove::getId, param.getId()));
        moldMove.setDeleteFlag(1);
        moldMove.setDeleteNote(param.getDeleteNote());
        //删除
        moldMoveService.saveOrUpdate(moldMove);
    }
    @Override
    public void remove(String moldCode) {
        //删除
//        moldFormService.remove(new LambdaQueryWrapper<MoldForm>().eq(MoldForm::getMoldCode, moldCode));
        MoldForm moldForm = moldFormService.getOne(new LambdaQueryWrapper<MoldForm>().eq(MoldForm::getMoldCode, moldCode));
        moldForm.setDeleteFlag(1);
        moldForm.setUseState(3);
        moldFormService.saveOrUpdate(moldForm);
    }
    @Override
    public List<MoldFormDTO> listAll(MoldFormQuery query) {
        return moldFormConvert.entity2ListDTO(moldFormService.list());
    }
    @Override
    public List<MoldFormDTO> listAll1(MoldFormQuery query) {
        List<MoldFormPO> moldFormPOS = moldFormService.findPageByQuery(null, query);
        return moldFormConvert.po2PageListDTO(moldFormPOS);
    }
}
