package cn.pluss.platform.controller.transferCash;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MerchantBankCard;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantTransferCash;
import cn.pluss.platform.merchantBankCard.MerchantBankCardService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.transferCash.MerchantTransferCashService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-09-26
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/merchantTransferCash")
public class MerchantTransferCashController extends BaseModelController<MerchantTransferCashService, MerchantTransferCash> {

    private final MerchantTransferCashService merchantTransferCashService;

    private final MerchantBankCardService merchantBankCardService;

    private final MerchantBaseInfoService merchantBaseInfoService;

    @GetMapping("index")
    public String index() {
        return "transferCash/list";
    }

    @RequestMapping("page")
    @ResponseBody
    public Result<Page<MerchantTransferCash>> page(Page<MerchantTransferCash> page, MerchantTransferCash merchantTransferCash) {
        QueryWrapper<MerchantTransferCash> queryWrapper = new QueryWrapper<MerchantTransferCash>().orderByDesc("createTime");
        if(StringUtil.isNotEmpty(merchantTransferCash.getUserName())){
            queryWrapper.like("userName",merchantTransferCash.getUserName());
        }
        if(StringUtil.isNotEmpty(merchantTransferCash.getCashNo())){
            queryWrapper.eq("cashNo",merchantTransferCash.getCashNo());
        }
        if(StringUtil.isNotEmpty(merchantTransferCash.getStatus())){
            queryWrapper.eq("status",merchantTransferCash.getStatus());
        }
        page = merchantTransferCashService.page(page, queryWrapper);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody MerchantTransferCash merchantTransferCash) {

        if (merchantTransferCash.getId() != null){
            merchantTransferCashService.updateById(merchantTransferCash);
        }else {
            merchantTransferCash.setCreateTime(new Date());
            merchantTransferCashService.save(merchantTransferCash);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    /**
     * 获取结算信息
     */
    @RequestMapping("settle/{userId}")
    @ResponseBody
    public Result settleInfo(@PathVariable("userId") Integer userId) {
        if(StringUtil.isEmpty(userId)){
            return ResultGenerator.genFailResult("未获取到用户ID！");
        }
        MerchantBankCard bankCard = merchantBankCardService.queryMerchantBankCard(String.valueOf(userId));
        if(bankCard == null){
            return ResultGenerator.genFailResult("当前商户编号有误！");
        }
        Map<String,Object> result = new HashMap<>();
        QueryWrapper<MerchantBaseInfo> queryWrapper = new QueryWrapper<MerchantBaseInfo>().eq("userId",userId);
        MerchantBaseInfo info = merchantBaseInfoService.getOne(queryWrapper);
        if(info != null){
            result.put("zfbAccount",info.getAliAccount());
        }
        result.put("bankCard", bankCard);
        return ResultGenerator.genSuccessResult(result);
    }
}
