/*
 * 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.fsrpcostinterestsettle.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.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.financialSupervision.modular.fsinterestsubsidysettle.entity.FsInterestSubsidySettle;
import com.unis.financialSupervision.modular.fsinterestsubsidysettle.service.FsInterestSubsidySettleService;
import com.unis.financialSupervision.modular.fsstoragesubsidysettle.entity.FsStorageSubsidySettle;
import com.unis.financialSupervision.modular.fsstoragesubsidysettle.service.FsStorageSubsidySettleService;
import com.unis.financialSupervision.modular.rotationCostSubsidySettle.entity.RotationCostSubsidySettle;
import com.unis.financialSupervision.modular.rotationCostSubsidySettle.mapper.RotationCostSubsidySettleMapper;
import com.unis.financialSupervision.modular.rotationCostSubsidySettle.service.RotationCostSubsidySettleService;
import io.swagger.annotations.ApiModelProperty;
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.fsrpcostinterestsettle.entity.FsRpCostInterestSettle;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.mapper.FsRpCostInterestSettleMapper;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.param.FsRpCostInterestSettleAddParam;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.param.FsRpCostInterestSettleEditParam;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.param.FsRpCostInterestSettleIdParam;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.param.FsRpCostInterestSettlePageParam;
import com.unis.financialSupervision.modular.fsrpcostinterestsettle.service.FsRpCostInterestSettleService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 财务监管-省级储备粮清算管理-轮换费用利息补贴清算表Service接口实现类
 *
 * @author ZeroLiYi
 * @date  2024/06/28 10:57
 **/
@Service
public class FsRpCostInterestSettleServiceImpl extends ServiceImpl<FsRpCostInterestSettleMapper, FsRpCostInterestSettle> implements FsRpCostInterestSettleService {

    @Resource
    private RotationCostSubsidySettleService rotationCostSubsidySettleService;

    @Resource
    private FsInterestSubsidySettleService fsInterestSubsidySettleService;

    @Resource
    private FsStorageSubsidySettleService fsStorageSubsidySettleService;

    @Resource
    private FsRpCostInterestSettleMapper fsRpCostInterestSettleMapper;

    @Override
    public List<FsRpCostInterestSettle> page(FsRpCostInterestSettlePageParam fsRpCostInterestSettlePageParam) {
        List<FsRpCostInterestSettle> fsRpCostInterestSettleList =new ArrayList<>();
        if(fsRpCostInterestSettlePageParam.getPlanYear()==null){
            fsRpCostInterestSettlePageParam.setPlanYear(LocalDate.now().getYear());
        }
        QueryWrapper<FsRpCostInterestSettle> fsRpCostInterestSettlequeryWrapper = new QueryWrapper<>();
        fsRpCostInterestSettlequeryWrapper.lambda().eq(FsRpCostInterestSettle::getPlanYear, fsRpCostInterestSettlePageParam.getPlanYear());
        if(this.list(fsRpCostInterestSettlequeryWrapper).size()>0){
            return this.list(fsRpCostInterestSettlequeryWrapper);
        }


        BigDecimal totalAllocation = new BigDecimal(0.00);/** 应拨小计 */
        BigDecimal storageCostAllocation = new BigDecimal(0.00);  /** 保管费用应拨数，单位：万元 */
        BigDecimal interestCostAllocation = new BigDecimal(0.00);    /** 利息费用应拨数，单位：万元 */
        BigDecimal rotationCostAllocation = new BigDecimal(0.00); /** 轮换费用应拨数，单位：万元 */
        // 根据年份查询 轮换费用补贴清算表 和  保管费用粮油补贴清算表
        List<Map<String,Object>> list= fsRpCostInterestSettleMapper.getOrgId(fsRpCostInterestSettlePageParam.getPlanYear());
        for (int j = 0; j < list.size(); j++) {
            FsRpCostInterestSettle fsRpCostInterestSettle=new FsRpCostInterestSettle();
            Map<String, Object> map = list.get(j);
            String orgId = map.getOrDefault("orgId", "").toString();
         // 应拨数小计 = 保管费用应拨数 + 利息费用应拨数 + 轮换费用应拨数   查询保管费用粮油补贴清算表
            QueryWrapper<FsStorageSubsidySettle> fsStorageSubsidySettlequeryWrapper = new QueryWrapper<>();
            fsStorageSubsidySettlequeryWrapper.lambda().eq(FsStorageSubsidySettle::getPlanYear, fsRpCostInterestSettlePageParam.getPlanYear());
            fsStorageSubsidySettlequeryWrapper.lambda().eq(FsStorageSubsidySettle::getOrgId, orgId);
            FsStorageSubsidySettle fsStorageSubsidySettle = fsStorageSubsidySettleService.getOne(fsStorageSubsidySettlequeryWrapper);
            // 保管费用应拨数
            if(fsStorageSubsidySettle!=null){
                // 保管费用应拨数 = 储备粮-原粮保管费用补贴（标准：0.12元/公斤）应拨数 + 成品粮保管费用补贴（标准：0.20元/公斤）应拨数 + 油料保管费用补贴（标准：0.40元/公斤）应拨数
                storageCostAllocation=fsStorageSubsidySettle.getStorageSubsidyStandardRawGrain().add(fsStorageSubsidySettle.getStorageSubsidyProcessedGrain()).add(fsStorageSubsidySettle.getStorageSubsidyOil());

            }
            QueryWrapper<FsInterestSubsidySettle> fsInterestSubsidySettleequeryWrapper = new QueryWrapper<>();
            fsInterestSubsidySettleequeryWrapper.lambda().eq(FsInterestSubsidySettle::getYear, fsRpCostInterestSettlePageParam.getPlanYear());
            fsInterestSubsidySettleequeryWrapper.lambda().eq(FsInterestSubsidySettle::getOrgId, orgId);
            FsInterestSubsidySettle fsInterestSubsidySettle = fsInterestSubsidySettleService.getOne(fsInterestSubsidySettleequeryWrapper);
            // 利息费用应拨数
            if(fsInterestSubsidySettle!=null){
                interestCostAllocation =fsInterestSubsidySettle.getInterestAllocated();
            }
            //biz_fs_rotation_cost_subsidy_settle
            QueryWrapper<RotationCostSubsidySettle> rotationCostSubsidySettlequeryWrapper = new QueryWrapper<>();
            rotationCostSubsidySettlequeryWrapper.lambda().eq(RotationCostSubsidySettle::getPlanYear, fsRpCostInterestSettlePageParam.getPlanYear());
            rotationCostSubsidySettlequeryWrapper.lambda().eq(RotationCostSubsidySettle::getOrgId, orgId);
            RotationCostSubsidySettle rotationCostSubsidySettle = rotationCostSubsidySettleService.getOne(rotationCostSubsidySettlequeryWrapper);
            // 轮换费用应拨数
            if(rotationCostSubsidySettle!=null){
                rotationCostAllocation =rotationCostSubsidySettle.getSubsidySubtotal();
            }
            // 应拨小计 = 保管费用应拨数 + 利息费用应拨数 + 轮换费用应拨数
            totalAllocation=storageCostAllocation.add(interestCostAllocation).add(rotationCostAllocation);
            fsRpCostInterestSettle.setStorageCostAllocation(storageCostAllocation);
            fsRpCostInterestSettle.setInterestCostAllocation(interestCostAllocation);
            fsRpCostInterestSettle.setRotationCostAllocation(rotationCostAllocation);
            fsRpCostInterestSettle.setTotalAllocation(totalAllocation);
            fsRpCostInterestSettleList.add(fsRpCostInterestSettle);
        }

        return fsRpCostInterestSettleList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(List<FsRpCostInterestSettleAddParam> fsRpCostInterestSettleAddParamList) {
        for (FsRpCostInterestSettleAddParam fsRpCostInterestSettleAddParam:fsRpCostInterestSettleAddParamList
             ) {
            QueryWrapper<FsRpCostInterestSettle> fsRpCostInterestSettlequeryWrapper = new QueryWrapper<>();
            fsRpCostInterestSettlequeryWrapper.lambda().eq(FsRpCostInterestSettle::getPlanYear, fsRpCostInterestSettleAddParam.getPlanYear());
            if(this.list(fsRpCostInterestSettlequeryWrapper).size()>0){
                this.remove(fsRpCostInterestSettlequeryWrapper);
            }
            FsRpCostInterestSettle fsRpCostInterestSettle = BeanUtil.toBean(fsRpCostInterestSettleAddParam, FsRpCostInterestSettle.class);
            this.save(fsRpCostInterestSettle);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(FsRpCostInterestSettleEditParam fsRpCostInterestSettleEditParam) {
        FsRpCostInterestSettle fsRpCostInterestSettle = this.queryEntity(fsRpCostInterestSettleEditParam.getId());
        BeanUtil.copyProperties(fsRpCostInterestSettleEditParam, fsRpCostInterestSettle);
        this.updateById(fsRpCostInterestSettle);
    }

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

    @Override
    public FsRpCostInterestSettle detail(FsRpCostInterestSettleIdParam fsRpCostInterestSettleIdParam) {
        return this.queryEntity(fsRpCostInterestSettleIdParam.getId());
    }

    @Override
    public FsRpCostInterestSettle queryEntity(String id) {
        FsRpCostInterestSettle fsRpCostInterestSettle = this.getById(id);
        if(ObjectUtil.isEmpty(fsRpCostInterestSettle)) {
            throw new CommonException("财务监管-省级储备粮清算管理-轮换费用利息补贴清算表不存在，id值为：{}", id);
        }
        return fsRpCostInterestSettle;
    }
}
