package com.jwipc.sys.logic.impl;

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.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.MoldSettingsConvert;
import com.jwipc.sys.dto.MoldSettingsDTO;
import com.jwipc.sys.entity.MoldForm;
import com.jwipc.sys.entity.MoldSettings;
import com.jwipc.sys.logic.MoldSettingsLogic;
import com.jwipc.sys.model.param.MoldSettingsParam;
import com.jwipc.sys.model.po.MoldSettingsPO;
import com.jwipc.sys.model.query.MoldSettingsQuery;
import com.jwipc.sys.service.IMoldFormService;
import com.jwipc.sys.service.IMoldSettingsService;
import lombok.RequiredArgsConstructor;
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
 * @create 2024-02-01 10:52
 */
@Service
@RequiredArgsConstructor
public class MoldSettingsLogicImpl implements MoldSettingsLogic {
    private final IMoldSettingsService moldSettingsService;
    private final MoldSettingsConvert moldSettingsConvert;
    private final IMoldFormService moldFormService;
    @Override
    public PageView findByPage(MoldSettingsQuery query) {
        final Page<MoldSettings> dbPage = PageUtil.toPage(query);

        List<MoldSettingsPO> moldSettingsPOS = moldSettingsService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(moldSettingsConvert.po2PageListDTO(moldSettingsPOS), dbPage.getTotal());
    }
    @Override
    public MoldSettingsDTO getById(Long id) {
        if (id != null) {
            MoldSettingsPO moldSettingsPO = moldSettingsService.getDataById(id);
            return moldSettingsConvert.po2DTO(moldSettingsPO);
        }else{
            return new MoldSettingsDTO();
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(MoldSettingsParam param) {
        //保存
        if (Objects.isNull(param.getId())) {
            final LambdaQueryWrapper<MoldSettings> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldSettings>().eq(
                    MoldSettings::getProductCode, param.getProductCode()).last(" limit 1");
//            if (ObjectUtil.isNotNull(moldSettingsService.getOne(resourcesLambdaQueryWrapper))) {
//                throw new DaoException("产品料号重复");
//            }
            param.setCreateTime(LocalDateTime.now());
            param.setCreater(LoginUtil.getUserId());
            param.setCreaterName(LoginUtil.getName());
        } else {
            final LambdaQueryWrapper<MoldSettings> resourcesLambdaQueryWrapper = new LambdaQueryWrapper<MoldSettings>().eq(
                    MoldSettings::getProductCode, param.getProductCode()).ne(MoldSettings::getId, param.getId()).last(" limit 1");
//            if (ObjectUtil.isNotNull(moldSettingsService.getOne(resourcesLambdaQueryWrapper))) {
//                throw new DaoException("产品料号重复");
//            }
        }
        MoldForm moldForm = moldFormService.findByCode(param.getMoldCode());
//        if(moldForm.getUseState() == 3){
//            throw new DaoException("该模具已报废，请选择其他模具！");
//        }
        moldForm.setUseState(2);
        moldFormService.saveOrUpdate(moldForm);
//        MoldFormQuery query = new MoldFormQuery();
//        query.setMoldCode(param.getMoldCode());
//        moldFormService.findPageByQuery(null,query);
        moldSettingsService.saveOrUpdate(moldSettingsConvert.param2Entity(param));
    }

    /**
     模具产品关联附件上传
     */
    @Override
    public void saveOnFile(Long id, String fileIds) {
        if (StrUtil.isEmpty(fileIds)) {
            return;
        }
        MoldSettings moldSettings = moldSettingsService.getById(id);
        moldSettings.setAttachment(fileIds);
        moldSettingsService.saveOrUpdate(moldSettings);
    }

    //删除
    @Override
    public void remove(String productCode) {
//        moldSettingsService.remove(new LambdaQueryWrapper<MoldSettings>().eq(MoldSettings::getProductCode, productCode));
        MoldSettings moldSettings = moldSettingsService.getOne(new LambdaQueryWrapper<MoldSettings>().eq(MoldSettings::getProductCode, productCode));
        moldSettings.setDeleteFlag(1L);
        moldSettingsService.saveOrUpdate(moldSettings);
    }
    //列表数据
    @Override
    public List<MoldSettingsDTO> listAll(MoldSettingsQuery query) {
        List<MoldSettingsPO> moldSettingsPOS = moldSettingsService.findPageByQuery(null, query);
        return moldSettingsConvert.po2PageListDTO(moldSettingsPOS);
    }

}
