package com.heyu.api.controller.mm;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.order.VVMySettlementAddRequest;
import com.heyu.api.alibaba.request.mm.order.VVMySettlementApprovalRequest;
import com.heyu.api.alibaba.request.mm.order.VVMySettlementRequest;
import com.heyu.api.alibaba.request.mm.order.resp.MyAccountDTO;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvMyAccountDao;
import com.heyu.api.data.dao.vv.VvMySettlementDao;
import com.heyu.api.data.entity.vv.VvMyAccountEntity;
import com.heyu.api.data.entity.vv.VvMySettlementEntity;
import com.heyu.api.data.enums.AccountOperateEnums;
import com.heyu.api.data.enums.AccountTypeEnums;
import com.heyu.api.data.enums.MySettlementStatusEnums;
import com.heyu.api.data.utils.BigDecimalUtil;
import com.heyu.api.data.utils.DingDingSettlementUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

// https://oapi.dingtalk.com/robot/send?access_token=4c36a91c1dbd4b3c2827cd8cd63f2342d2da7c6d7ebc5f62cf06197034b12bec
@Slf4j
@RestController
@RequestMapping("/mm/my/settlement")
public class AdminMySettlementController {

    @Autowired
    private VvMySettlementDao vvMySettlementDao;

    @Autowired
    private VvMyAccountDao vvMyAccountDao;

    /***
     * 列表
     * http://localhost:8888/mm/my/settlement/list
     */
    @Describe("公款列表")
    @RequestMapping("/list")
    public R list(@RequestBody VVMySettlementRequest vvMySettlementRequest) {
        log.info("AdminMySettlementController json :{}", JSONObject.toJSONString(vvMySettlementRequest));
        VvMyAccountEntity vvMyAccountEntity = vvMyAccountDao.selectVvMyAccount(AccountTypeEnums.common.getType());
        PPageUtils pageUtils = PPageUtils.startPage(vvMySettlementRequest.getPageNum(), vvMySettlementRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvMySettlementDao.selectByCondition(page,
                                vvMySettlementRequest.getStatus(),
                                vvMySettlementRequest.getMinAmount(),
                                vvMySettlementRequest.getMaxAmount(),
                                vvMySettlementRequest.getMinCreateTimestamp(),
                                vvMySettlementRequest.getMaxCreateTimestamp()
                        );
                    }
                });

        MyAccountDTO myAccountDTO = new MyAccountDTO();
        myAccountDTO.setVvMyAccountEntity(vvMyAccountEntity);
        myAccountDTO.setPageUtils(pageUtils);
        return R.ok().setData(myAccountDTO);
    }

    /***
     * 插入数据
     * http://localhost:8888/mm/my/settlement/add
     */
    @Describe("公款添加")
    @RequestMapping("/add")
    public R add(@RequestBody VVMySettlementAddRequest vvMySettlementEntity) {
        String userName = vvMySettlementEntity.getUserName();
        vvMySettlementEntity.setApplyUserName(userName);
        vvMySettlementEntity.setType(AccountTypeEnums.common.getType());
        if (AccountOperateEnums.plus.getSign().equals(vvMySettlementEntity.getOperate())) {
            VvMyAccountEntity vvMyAccountEntity = vvMyAccountDao.selectVvMyAccount(AccountTypeEnums.common.getType());
            vvMyAccountEntity.setAmount(vvMySettlementEntity.getAmount().add(vvMyAccountEntity.getAmount()));
            vvMyAccountEntity.setRechargeAmount(vvMySettlementEntity.getAmount().add(vvMyAccountEntity.getRechargeAmount()));
            vvMyAccountDao.updateVvMyAccountById(vvMyAccountEntity);
            String content = vvMySettlementEntity.getApplyUserName()
                    + "，充值了:" + vvMySettlementEntity.getAmount()
                    + "，账户余额:" + vvMyAccountEntity.getAmount();
            DingDingSettlementUtils.sendText(content, "账户充值");
            vvMySettlementEntity.setStatus(MySettlementStatusEnums.no_approval.getStatus());
            vvMySettlementEntity.setRemainAmount(vvMySettlementEntity.getAmount());
        } else {
            String content = vvMySettlementEntity.getApplyUserName()
                    + "，提交了报销单，报销金额:" + vvMySettlementEntity.getAmount()
                    + "，申请原因：" + vvMySettlementEntity.getApplyReason();
            DingDingSettlementUtils.sendText(content, "报销申请");
            vvMySettlementEntity.setStatus(MySettlementStatusEnums.apply_for_approval.getStatus());
        }
        vvMySettlementEntity.setCreateTimestamp(System.currentTimeMillis());
        vvMySettlementEntity.setModifyTimestamp(System.currentTimeMillis());

        vvMySettlementDao.insertVvMySettlement(vvMySettlementEntity);

        return R.ok().setData(vvMySettlementEntity);
    }

    /***
     * 审批
     * apply_for_approval:申请审批，agree_approval: 同意审批 ,reject_approval：拒绝审批
     *
     *
     *
     * http://localhost:8888/mm/my/settlement/approval
     *
     *
     *
     */
    @Describe("公款审批")
    @RequestMapping("/approval")
    public R approval(@RequestBody VVMySettlementApprovalRequest vvMySettlementApprovalRequest) {
        VvMySettlementEntity vvMySettlement = vvMySettlementDao.selectVvMySettlementById(vvMySettlementApprovalRequest.getId());
        VvMyAccountEntity vvMyAccountEntity = vvMyAccountDao.selectVvMyAccount(AccountTypeEnums.common.getType());
        vvMySettlement.setApprovalUserName(vvMySettlementApprovalRequest.getUserName());


        if (!MySettlementStatusEnums.apply_for_approval.getStatus().equals(vvMySettlement.getStatus())) {
            return R.error("状态不对");
        }

        if (MySettlementStatusEnums.agree_approval.getStatus().equals(vvMySettlementApprovalRequest.getStatus())) {
            BigDecimal acountAmount = vvMyAccountEntity.getAmount();
            BigDecimal lastRemainAmount = BigDecimalUtil.subtract(acountAmount, vvMySettlement.getAmount());
            vvMySettlement.setRemainAmount(lastRemainAmount);

            vvMyAccountEntity.setAmount(lastRemainAmount);
            vvMyAccountEntity.setSubAmount(vvMyAccountEntity.getSubAmount().add(vvMySettlement.getAmount()));
            vvMyAccountDao.updateVvMyAccountById(vvMyAccountEntity);
            String content = vvMySettlementApprovalRequest.getUserName()
                    + "，同意了:" + vvMySettlement.getApplyUserName()
                    + "，申请的:" + vvMySettlement.getAmount()
                    + "，申请原因:" + vvMySettlement.getApplyReason()
                    + "，账户余额:" + vvMyAccountEntity.getAmount();
            DingDingSettlementUtils.sendText(content, "同意审批");
        } else if (MySettlementStatusEnums.reject_approval.getStatus().equals(vvMySettlementApprovalRequest.getStatus())) {
            String content = vvMySettlementApprovalRequest.getUserName()
                    + "，拒绝了:" + vvMySettlement.getApplyUserName()
                    + "，申请的" + vvMySettlement.getAmount()
                    + "，账户余额:" + vvMyAccountEntity.getAmount()
                    + "，拒绝原因:" + vvMySettlementApprovalRequest.getRejectReason();
            DingDingSettlementUtils.sendText(content, "拒绝审批");
            vvMySettlement.setRejectReason(vvMySettlementApprovalRequest.getRejectReason());
            vvMySettlement.setRemainAmount(vvMyAccountEntity.getAmount());
        }
        vvMySettlement.setModifyTimestamp(System.currentTimeMillis());
        vvMySettlement.setStatus(vvMySettlementApprovalRequest.getStatus());
        vvMySettlementDao.updateVvMySettlementById(vvMySettlement);
        return R.ok().setData(vvMySettlement);
    }

}
