package com.hzs.system.config.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.config.BdBank;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdBankService;
import com.hzs.system.config.vo.BdBankVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 银行配置
 * @Author: yuhui
 * @Time: 2022/10/25 14:14
 * @Classname: BdBankController
 * @PackageName: com.hzs.system.config.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/bank")
public class BdBankController extends BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private IBdBankService bankService;

    @DubboReference
    private ITransactionServiceApi transactionServiceApi;

    /**
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(BdBank bank) {
        QueryWrapper<BdBank> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(bank.getBankName())) {
            queryWrapper.eq("BANK_NAME", bank.getBankName());
        }
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        startPage();
        List<BdBank> list = bankService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 银行下拉选列表
     *
     * @return
     */
    @GetMapping("listNoPage")
    public TableDataInfo listNoPage(BdBank bank) {
        QueryWrapper<BdBank> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        List<BdBank> list = bankService.list(queryWrapper);
        List<BdBankVo> listVo = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(item -> listVo.add(BeanUtil.copyProperties(item, BdBankVo.class)));
        }
        return getDataTable(listVo);
    }

    /**
     * 新增银行信息
     *
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody BdBank bdBank) {
        bdBank.setPkCreator(SecurityUtils.getUserId());
        bdBank.setCreationTime(new Date());
        bdBank.setPkCountry(SecurityUtils.getPkCountry());
        R<Integer> transaction = transactionServiceApi.createTransaction(SecurityUtils.getPkCountry(), ETransactionKey.BANK, bdBank.getBankName(), EYesNo.NO,EYesNo.NO);
        bdBank.setPkTransaction(transaction.getData());
        return AjaxResult.success(bankService.save(bdBank));
    }

    /**
     * 编辑银行信息
     *
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody BdBank bdBank) {
        QueryWrapper<BdBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_ID",bdBank.getPkId());
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        BdBank bank = bankService.getOne(queryWrapper);
        BeanUtil.copyProperties(bdBank,bank);
        bank.setPkModified(SecurityUtils.getUserId());
        bank.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_BANK + bdBank.getPkId());
        transactionServiceApi.updateTransaction(SecurityUtils.getPkCountry(),bdBank.getBankName(),bank.getPkTransaction(), true);
        UpdateWrapper<BdBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID",bdBank.getPkId());
        updateWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        bankService.update(bank,updateWrapper);
        //bankService.updateById(bank)
        return AjaxResult.success();
    }

    /**
     * 删除银行信息
     *
     * @param bdBank
     * @return
     */
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody BdBank bdBank) {
        QueryWrapper<BdBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_ID",bdBank.getPkId());
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        BdBank bank = bankService.getOne(queryWrapper);
        // TODO 校验是否有用到银行卡的地方 如已使用则无法删除
        UpdateWrapper<BdBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", bdBank.getPkId());
        updateWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        bankService.update(updateWrapper);
        // 删除翻译
        transactionServiceApi.removeTransactionByPkId(Arrays.asList(bank.getPkTransaction().longValue()));
        return AjaxResult.success();
    }

    /**
     * 详情
     *
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Integer pkId) {
        QueryWrapper<BdBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_ID",pkId);
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        BdBank BdBank = bankService.getOne(queryWrapper);
        return AjaxResult.success(BdBank);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("find-all")
    public AjaxResult findAll(){
        List<BdBank> list = bankService.list();
        return AjaxResult.success(list);
    }
}
