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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.enums.EDelFlag;
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.config.service.IBdBankService;
import com.hzs.system.config.vo.BdBankVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 银行配置
 */
@Slf4j
@RestController
@RequestMapping("/manage/bank")
public class BdBankController extends BaseController {

    @Autowired
    private IBdBankService iBdBankService;

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

    /**
     * 银行下拉选列表
     */
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage() {
        LambdaQueryWrapper<BdBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdBank::getPkCountry, SecurityUtils.getPkCountry());
        List<BdBank> list = iBdBankService.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());
        return AjaxResult.success(iBdBankService.save(bdBank));
    }

    /**
     * 编辑银行信息
     *
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdBank bdBank) {
        BdBank bank = iBdBankService.getById(bdBank.getPkId());
        BeanUtil.copyProperties(bdBank, bank);
        bank.setPkModified(SecurityUtils.getUserId());
        bank.setModifiedTime(new Date());
        return AjaxResult.success(iBdBankService.updateById(bank));
    }

    /**
     * 删除银行信息
     *
     * @param bdBank
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody BdBank bdBank) {
        LambdaUpdateWrapper<BdBank> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdBank::getPkId, bdBank.getPkId());
        updateWrapper.set(BdBank::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdBank::getModifiedTime, new Date());
        updateWrapper.set(BdBank::getDelFlag, EDelFlag.DELETE.getValue());
        iBdBankService.update(updateWrapper);
        return AjaxResult.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public AjaxResult detail(Integer pkId) {
        return AjaxResult.success(iBdBankService.getCurrency(pkId));
    }

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

}
