package org.jeecg.modules.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import org.jeecg.modules.basedata.entity.FdCustomer;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.basedata.service.IFdCustomerService;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.finance.entity.FdBalancePaymentSettlement;
import org.jeecg.modules.finance.entity.FdBalancePaymentSettlementItem;
import org.jeecg.modules.finance.mapper.FdBalancePaymentSettlementMapper;
import org.jeecg.modules.finance.service.IFdBalancePaymentSettlementItemService;
import org.jeecg.modules.finance.service.IFdBalancePaymentSettlementService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 尾款结算
 * @author： tanj
 * @date：   2019-07-20
 * @version： V1.0
 */
@Service
@Transactional
public class FdBalancePaymentSettlementServiceImpl extends ServiceImpl<FdBalancePaymentSettlementMapper, FdBalancePaymentSettlement> implements IFdBalancePaymentSettlementService {

    @Resource
    private FdBalancePaymentSettlementMapper fdBalancePaymentSettlementMapper;

    @Resource
    private IFdBalancePaymentSettlementItemService fdBalancePaymentSettlementItemService;

    @Resource
    private IFdBalancePaymentSettlementService fdBalancePaymentSettlementService;

    /**
     * @Author tanj
     * @Description //TODO 分页查询
     * @Date 11:11 2019/7/20
     * @Param [page, queryWrapper]
     * @return com.baomidou.mybatisplus.core.metadata.IPage<org.jeecg.modules.finance.entity.FdBalancePaymentSettlement>
     **/
    @Override
    public IPage<FdBalancePaymentSettlement> findByPage(IPage<FdBalancePaymentSettlement> page, QueryWrapper<FdBalancePaymentSettlement> queryWrapper) {
        return fdBalancePaymentSettlementMapper.findByPage(page, queryWrapper);
    }

    /**
     * @Author tanj
     * @Description //TODO 新增尾款、明细
     * @Date 11:10 2019/7/20
     * @Param [fdBalancePaymentSettlement]
     * @return void
     **/
    @Override
    public void addBalancePaymentSettlement(FdBalancePaymentSettlement fdBalancePaymentSettlement) {
        //  新增尾款
        fdBalancePaymentSettlementService.save(fdBalancePaymentSettlement);
        List<FdBalancePaymentSettlementItem> settlementItemList = fdBalancePaymentSettlement.getBalancePaymentSettlementItemList();
        for ( FdBalancePaymentSettlementItem fdBalancePaymentSettlementItem : settlementItemList ) {
            fdBalancePaymentSettlementItem.setBalancePaymentSettlementId(fdBalancePaymentSettlement.getId());
            //fdBalancePaymentSettlementItemService.save(fdBalancePaymentSettlementItem);
        }
        //  再全新增一次
        fdBalancePaymentSettlementItemService.saveBatch(settlementItemList);

    }

    /**
     * @Author tanj
     * @Description //TODO 修改尾款、明细
     * @Date 11:13 2019/7/20
     * @Param [fdBalancePaymentSettlement]
     * @return void
     **/
    @Override
    public void updateBalancePaymentSettlement(FdBalancePaymentSettlement fdBalancePaymentSettlement) {
        updateById(fdBalancePaymentSettlement);
        //  先将根据传入尾款明细id删除已有数据
        //  1.1根据采购订单主键先删除
        Map<String, Object> map = Maps.newHashMap();
        map.put("balance_payment_settlement_id", fdBalancePaymentSettlement.getId());
        fdBalancePaymentSettlementItemService.removeByMap(map);
        List<FdBalancePaymentSettlementItem> settlementItemList = fdBalancePaymentSettlement.getBalancePaymentSettlementItemList();
        for ( FdBalancePaymentSettlementItem fdBalancePaymentSettlementItem : settlementItemList ) {
            fdBalancePaymentSettlementItem.setBalancePaymentSettlementId(fdBalancePaymentSettlement.getId());
        }
        //  再全新增一次
        fdBalancePaymentSettlementItemService.saveBatch(settlementItemList);
    }

    /**
     * @Author tanj
     * @Description //TODO 弃审核数据 ,使其 状态为0
     * @Date 14:25 2019/7/20
     * @Param [ids]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> auditBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unAudit = " ";//可审计
        String audit = " ";//已审计
        //根据多个id查询报价对象集合
        List<FdBalancePaymentSettlement> balancePaymentSettlementList = fdBalancePaymentSettlementMapper.selectBatchIds(ids);
        //修改
        if (balancePaymentSettlementList.size() == 0) {
            audit += "未找到修改数据</br>";
        } else {
            //判断哪些可关闭哪些不可关闭
            for (FdBalancePaymentSettlement settlement : balancePaymentSettlementList) {
                if (settlement.getStatus().equals("0")) {
                    audit += settlement.getStatementNo() + "，";
                } else {
                    unAudit += settlement.getStatementNo() + "，";
                }
            }
            //可关闭存在则操作数据库修改
            if (!unAudit.equals("")) {
                fdBalancePaymentSettlementMapper.auditBatch(ids);
            }
        }
        //去掉最后一个"，"号
        audit = audit.substring(0, audit.length() - 1);
        unAudit = unAudit.substring(0, unAudit.length() - 1);
        //加关键词
        if (!unAudit.equals("")) {
            unAudit = unAudit + "弃审核成功";
        }
        if (!audit.equals("")) {
            audit = "其中:" + audit + "已处于未审核状态。<br> ";
        }
        map.put("msg", unAudit + audit);
        return map;
    }

    /**
     * @Author tanj
     * @Description //TODO 审核数据 ,使其 状态为1
     * @Date 14:25 2019/7/20
     * @Param [ids]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @Override
    public Map<String, Object> reviewBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unReview = " "; //未审核
        String review = " "; //已审核
        //根据多个id查询报价对象集合
        List<FdBalancePaymentSettlement> fdProjectFileList = fdBalancePaymentSettlementMapper.selectBatchIds(ids);
        //修改
        if (fdProjectFileList == null || fdProjectFileList.isEmpty()) {
            review += "未找到修改数据</br>";
        } else {
            //判断哪些已审核哪些未审核
            for (FdBalancePaymentSettlement fdProjectFile : fdProjectFileList) {
                if (fdProjectFile.getStatus().equals("0")) {
                    unReview += fdProjectFile.getStatementNo() + ",";
                }
                if (fdProjectFile.getStatus().equals("1")) {
                    review += fdProjectFile.getStatementNo() + ",";
                }
                if (fdProjectFile.getStatus().equals("2")) {
                    throw new RuntimeException("已关闭！无法审核");
                }
                if (fdProjectFile.getStatus().equals("3")) {
                    throw new RuntimeException("已转合同！无法审核");
                }
            }
            //去掉最后一个","号
            unReview = unReview.substring(0, unReview.length() - 1);
            review = review.substring(0, review.length() - 1);
            //操作数据库将未审核的修改成已审核
            if (!unReview.equals("")) {
                fdBalancePaymentSettlementMapper.reviewBatch(ids);
            }
        }
        //加关键词
        if (!unReview.equals("")) {
            unReview = unReview + "审核成功";
        }
        if (!review.equals("")) {
            review = "其中：" + review + "已审核。<br>";
        }
        map.put("msg", unReview + review);
        return map;
    }
}
