package com.jwipc.inquiry.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.inquiry.convert.InquiryTemplateCostConvert;
import com.jwipc.inquiry.dto.InquiryCostTemplateDTO;
import com.jwipc.inquiry.dto.InquiryTemplateDTO;
import com.jwipc.inquiry.dto.InquiryTemplateFieldRelationDTO;
import com.jwipc.inquiry.entity.InquiryCostTemplate;
import com.jwipc.inquiry.entity.InquiryTemplate;
import com.jwipc.inquiry.entity.InquiryTemplateFieldRelation;
import com.jwipc.inquiry.logic.InquiryCostTemplateLogic;
import com.jwipc.inquiry.model.po.InquiryCostTemplatePO;
import com.jwipc.inquiry.model.po.InquiryTemplatePO;
import com.jwipc.inquiry.model.query.InquiryCostTemplateQuery;
import com.jwipc.inquiry.service.IInquiryCostTemplateService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/2/21 13:39
 * @desc
 */
@Service
@RequiredArgsConstructor
public class InquiryCostTemplateLogicImpl implements InquiryCostTemplateLogic {
    private final IInquiryCostTemplateService inquiryCostTemplateService;
    private final InquiryTemplateCostConvert inquiryTemplateCostConvert;
    @Override
    public PageView findByPage(InquiryCostTemplateQuery query) {
        final Page<InquiryCostTemplate> dbPage = PageUtil.toPage(query);

        List<InquiryCostTemplatePO> inquiryCostTemplatePOS = inquiryCostTemplateService.findPageByQuery(dbPage, query);

        List<InquiryCostTemplateDTO> inquiryCostTemplateDTOS = inquiryTemplateCostConvert.po2DTOList(inquiryCostTemplatePOS);
        return PageUtil.getPV(inquiryCostTemplateDTOS, dbPage.getTotal());
    }

    @Override
    public InquiryCostTemplateDTO getById(Long id) {
        InquiryCostTemplate inquiryTemplate = inquiryCostTemplateService.getById(id);
        return inquiryTemplateCostConvert.entity2Dto(inquiryTemplate);
    }

    @Override
    public void saveOrUpdate(InquiryCostTemplateDTO param) {
        //保存
        if (param.getId() == null) {
            if (ObjectUtil.isNotNull(inquiryCostTemplateService.getOne(new LambdaQueryWrapper<InquiryCostTemplate>().eq(
                    InquiryCostTemplate::getTemplateCode, param.getTemplateCode()).last(" limit 1")))) {
                throw new DaoException("模板编码已经存在, 请检查");
            }
            param.setCreateTime(LocalDateTime.now());
            param.setCreateUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        } else {
            if (ObjectUtil.isNotNull(inquiryCostTemplateService.getOne(new LambdaQueryWrapper<InquiryCostTemplate>().eq(
                            InquiryCostTemplate::getTemplateCode, param.getTemplateCode()).ne(InquiryCostTemplate::getId, param.getId())
                    .last(" limit 1")))) {
                throw new DaoException("模板编码已经存在, 请检查");
            }
            param.setUpdateTime(LocalDateTime.now());
            param.setUpdateUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());

        }
        inquiryCostTemplateService.saveOrUpdate(inquiryTemplateCostConvert.dto2Entity(param));
    }

    @Override
    public void updateDeleteFlag(Long id) {
        InquiryCostTemplate inquiryCostTemplate = new InquiryCostTemplate();
        inquiryCostTemplate.setId(id);
        inquiryCostTemplate.setDeleteFlag(1);
        inquiryCostTemplateService.updateById(inquiryCostTemplate);
    }

    @Override
    public List<InquiryCostTemplateDTO> listAll(InquiryCostTemplateQuery query) {
        List<InquiryCostTemplatePO> inquiryCostTemplatePOS = inquiryCostTemplateService.findPageByQuery(null, query);
        return inquiryTemplateCostConvert.po2DTOList(inquiryCostTemplatePOS);
    }

    /**
     * 根据采购组编码获取成本模板且包含全部采购组
     *
     * @param purchaseGroupCode 采购组编码
     */
    @Override
    public List<InquiryCostTemplateDTO> getAllListByPurchaseGroupCode(String purchaseGroupCode) {
        List<InquiryCostTemplatePO> inquiryCostTemplatePOS = inquiryCostTemplateService.getAllListByPurchaseGroupCode(purchaseGroupCode);
        return inquiryTemplateCostConvert.po2DTOList(inquiryCostTemplatePOS);
    }
}
