package com.jwipc.sys.logic.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
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.logic.MoldMoveLogic;
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 MoldMoveLogicImpl implements MoldMoveLogic {

    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);

        return PageUtil.getPV(moldFormConvert.po2PageListDTO(moldFormPOS), dbPage.getTotal());
    }
//    @Override
//    public PageView moldMoveFindByPage(MoldMoveQuery query) {
//        final Page<MoldMove> dbPage = PageUtil.toPage(query);
//
//        List<MoldMovePO> moldMovePOS = moldMoveService.findPageByQuery(dbPage, query);
//
//        return PageUtil.getPV(moldMoveConvert.po2PageListDTO(moldMovePOS), dbPage.getTotal());
//    }
    @Override
    public MoldMoveDTO getById(Long id) {
        if (id != null) {
            MoldMovePO moldMovePO = moldMoveService.getDataById(id);
            return moldMoveConvert.po2DTO(moldMovePO);
        }else{
            return new MoldMoveDTO();
        }
    }
    @Override
    public MoldMoveDTO moldMoveGetById(Long id) {
        if (id != null) {
            MoldMovePO moldMovePO = moldMoveService.getDataById(id);
            return moldMoveConvert.po2DTO(moldMovePO);
        }else{
            return new MoldMoveDTO();
        }
    }
    @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.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");
        }
        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 ((double) param.getActualUseTimes() / param.getUseTimes() >= 0.8) {
                param.setIsCallPolice(1);
            } else {
                param.setIsCallPolice(0);
            }
            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 ((double) param.getActualUseTimes() / param.getUseTimes() >= 0.8) {
                param.setIsCallPolice(1);
            } else {
                param.setIsCallPolice(0);
            }

        }
        moldFormService.saveOrUpdate(moldFormConvert.param2Entity(param));
    }
    @Override
    public void moldMoveRemove(Long id) {
        //删除
        moldMoveService.remove(new LambdaQueryWrapper<MoldMove>().eq(MoldMove::getId, id));
    }
    @Override
    public void remove(String moldCode) {
        //删除
        moldFormService.remove(new LambdaQueryWrapper<MoldForm>().eq(MoldForm::getMoldCode, moldCode));
    }
    @Override
    public List<MoldFormDTO> listAll() {
        return moldFormConvert.entity2ListDTO(moldFormService.list());
    }
}
