package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.base.entity.CommonRoleResource;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.service.ICommonRoleResourceService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.winsea.svc.base.security.entity.User;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.RepaymentManagementMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IRepaymentManagementService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.ITradeWarehouseReceiptApplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 贸易仓单还款管理 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-11-09
 */
@Service
public class RepaymentManagementServiceImpl extends ServiceImpl<RepaymentManagementMapper, RepaymentManagement> implements IRepaymentManagementService {

    @Autowired
    private ITradeWarehouseReceiptApplService tradeWarehouseReceiptApplService;
    @SofaReference
    private ICommonRoleResourceService roleResourceService;

    /**
     * 还款列表
     * @param repaymentManagement
     * @return
     */
    @Override
    public Page<RepaymentManagement> selectRepaymentManagement(RepaymentManagement repaymentManagement){
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (repaymentManagement.getCurrentPage() - 1)
                * repaymentManagement.getPageSize());
        //公司id
        pageView.put("compId", repaymentManagement.getCompId());
        pageView.put("tradeWarehouseReceiptId", repaymentManagement.getTradeWarehouseReceiptId());
        pageView.put("contractNo", repaymentManagement.getContractNo());
        pageView.put("pageSize", repaymentManagement.getPageSize());
        pageView.put("currentPage", repaymentManagement.getCurrentPage());
        // 查询还款总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<RepaymentManagement> dataList = baseMapper.getListByCondition(pageView);
        Page<RepaymentManagement> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(repaymentManagement.getCurrentPage());
        page.setSize(repaymentManagement.getPageSize());
        return page;
    }

    /**
     * 还款
     * @param repaymentManagement
     * @return
     */
    @Override
    public String repaymentMoney(RepaymentManagement repaymentManagement){
        //新增主键id
        repaymentManagement.setId(IdGenerator.generateUUID());
        //查询贸易仓单信息
        TradeWarehouseReceiptAppl tradeWarehouseReceiptAppl = tradeWarehouseReceiptApplService.selectOne(new EntityWrapper<TradeWarehouseReceiptAppl>()
                .eq("id",repaymentManagement.getTradeWarehouseReceiptId()));
        tradeWarehouseReceiptAppl.setAmountToRepaid(tradeWarehouseReceiptAppl.getAmountToRepaid() - repaymentManagement.getRepaymentEdAmount());
        repaymentManagement.setRepaymentIngAmount(tradeWarehouseReceiptAppl.getAmountToRepaid());
        tradeWarehouseReceiptAppl.setAmountRepaid(tradeWarehouseReceiptAppl.getAmountDue() - tradeWarehouseReceiptAppl.getAmountToRepaid());
        // 操作主表数据
        this.insert(repaymentManagement);
        tradeWarehouseReceiptApplService.updateById(tradeWarehouseReceiptAppl);
        return "OK";
    }



    private List<String> getResourceIdList() {
        User currentUser = AuthSecurityUtils.getCurrentUserInfo();
        // 当前登录人主要角色
        return roleResourceService.getBindResourcesByUserId(currentUser.getUserId()).stream()
                .map(CommonRoleResource::getResourceId).collect(Collectors.toList());
    }

}
