/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.financialSupervision.modular.priceApproval.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.common.constant.DBConstant;
import com.unis.financialSupervision.modular.priceApproval.entity.DataCPZ;
import com.unis.financialSupervision.modular.priceApproval.param.*;
import com.unis.financialSupervision.modular.priceApprovalInspection.entity.PriceApprovalInspection;
import com.unis.financialSupervision.modular.priceApprovalInspection.mapper.PriceApprovalInspectionMapper;
import com.unis.financialSupervision.modular.priceApprovalSettle.entity.PriceApprovalSettle;
import com.unis.financialSupervision.modular.priceApprovalSettle.mapper.PriceApprovalSettleMapper;
import com.unis.financialSupervision.modular.priceApprovalThreeConsistence.entity.PriceApprovalThreeConsistence;
import com.unis.financialSupervision.modular.priceApprovalThreeConsistence.mapper.PriceApprovalThreeConsistenceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.exception.CommonException;
import com.unis.common.page.CommonPageRequest;
import com.unis.financialSupervision.modular.priceApproval.entity.PriceApproval;
import com.unis.financialSupervision.modular.priceApproval.mapper.PriceApprovalMapper;
import com.unis.financialSupervision.modular.priceApproval.service.PriceApprovalService;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 方案价格核定Service接口实现类
 *
 * @author QSHan
 * @date 2024/07/08 15:08
 **/
@Service
@DS(DBConstant.DS_DEPOT)
public class PriceApprovalServiceImpl extends ServiceImpl<PriceApprovalMapper, PriceApproval> implements PriceApprovalService {
    @Resource
    private PriceApprovalMapper priceApprovalMapper;
    @Resource
    private PriceApprovalInspectionMapper inspectionMapper;
    @Resource
    private PriceApprovalThreeConsistenceMapper threeConsistenceMapper;
    @Resource
    private PriceApprovalSettleMapper settleMapper;

    @Override
    public Page<PriceApproval> page(PriceApprovalPageParam priceApprovalPageParam) {
        QueryWrapper<PriceApproval> queryWrapper = new QueryWrapper<>();
        // 检索条件
        queryWrapper.ne("pa.`status`", 0);
        if (ObjectUtils.isNotEmpty(priceApprovalPageParam.getOrgId())) {
            queryWrapper.eq("pa.org_id", priceApprovalPageParam.getOrgId());
        }
        // 排序
        if (ObjectUtil.isAllNotEmpty(priceApprovalPageParam.getSortField(), priceApprovalPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(priceApprovalPageParam.getSortOrder());
            queryWrapper.orderBy(true, priceApprovalPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(priceApprovalPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByDesc(PriceApproval::getId);
        }
        queryWrapper.groupBy("pa.id");
        return priceApprovalMapper.pageList(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(PriceApprovalAddParam priceApprovalAddParam) {
        PriceApproval priceApproval = BeanUtil.toBean(priceApprovalAddParam, PriceApproval.class);
        this.save(priceApproval);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PriceApprovalEditParam priceApprovalEditParam) {
        // 修改主表状态
        PriceApproval priceApproval = this.queryEntity(priceApprovalEditParam.getId());
        BeanUtil.copyProperties(priceApprovalEditParam, priceApproval);
        this.updateById(priceApproval);

        // 修改质检信息
        List<PriceApprovalInspection> inspections = priceApprovalEditParam.getInspections();
        if (CollectionUtils.isNotEmpty(inspections)) {
            inspections.forEach(inspection -> inspectionMapper.updateById(inspection));
        }
        // 修改三账一致性
        List<PriceApprovalThreeConsistence> threeConsistences = priceApprovalEditParam.getThreeConsistences();
        if (CollectionUtils.isNotEmpty(threeConsistences)) {
            threeConsistences.forEach(threeConsistence -> threeConsistenceMapper.updateById(threeConsistence));
        }
        // 修改结算信息,入库费用情况
        List<PriceApprovalSettle> settles = priceApprovalEditParam.getSettles();
        if (CollectionUtils.isNotEmpty(settles)) {
            settles.forEach(settle -> settleMapper.updateById(settle));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PriceApprovalIdParam> priceApprovalIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(priceApprovalIdParamList, PriceApprovalIdParam::getId));
    }

    @Override
    public PriceApproval detail(PriceApprovalIdParam priceApprovalIdParam) {
        Integer id = priceApprovalIdParam.getId();
        // 查询主表关联数据
        PriceApproval priceApproval = priceApprovalMapper.queryEntityById(id);
        // 合同信息
        List<BusinessContractParam> contractInfo = priceApprovalMapper.getContractInfo(priceApproval.getContractId());
        if (CollectionUtils.isNotEmpty(contractInfo)) {
            priceApproval.setContractList(contractInfo);
        }
        // 质检信息
        List<PriceApprovalInspection> inspections = priceApprovalMapper.getQualityInfoBySaveJoin(id);
        if (CollectionUtils.isNotEmpty(inspections)) {
            priceApproval.setInspections(inspections);
        }
        // 三账一致性
        List<PriceApprovalThreeConsistence> threeConsistences = priceApprovalMapper.threeConsistences(id);
        if (CollectionUtils.isNotEmpty(threeConsistences)) {
            priceApproval.setThreeConsistences(threeConsistences);
        }
        // 结算信息,入库费用情况
        List<PriceApprovalSettle> settleList = priceApprovalMapper.getApprovalSettleList(id);
        if (CollectionUtils.isNotEmpty(settleList)) {
            priceApproval.setSettles(settleList);
        }
        return priceApproval;
    }

    @Override
    public PriceApproval queryEntity(Integer id) {
        PriceApproval priceApproval = this.getById(id);
        if (ObjectUtil.isEmpty(priceApproval)) {
            throw new CommonException("方案价格核定不存在，id值为：{}", id);
        }
        return priceApproval;
    }

    @Override
    public List<DataCPZ> getApprovalThreeRKMX(Integer orgId) {
        return priceApprovalMapper.getApprovalThreeRKMX(orgId);
    }
}
