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


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.enums.EDelFlag;
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.BdWithdrawalAuto;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdWithdrawalAutoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 自动提现明细表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-08-31
 */
@Slf4j
@RestController
@RequestMapping("/manage/withdrawal-auto")
public class BdWithdrawalAutoController extends BaseController {

    @Autowired
    private IBdWithdrawalAutoService withdrawalAutoService;
    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * @description: 查询自动提现设置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bdWithdrawal]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(BdWithdrawalAuto withdrawalAuto) {
        startPage();
        withdrawalAuto.setPkCountry(SecurityUtils.getPkCountry());
        QueryWrapper<BdWithdrawalAuto> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY",SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(withdrawalAuto.getPkWithdrawal())){
            queryWrapper.eq("PK_WITHDRAWAL",withdrawalAuto.getPkWithdrawal());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalAuto> list = withdrawalAutoService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增自动提现设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdWithdrawal]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdWithdrawalAuto withdrawalAuto) {
        withdrawalAuto.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalAuto.setPkCreator(SecurityUtils.getUserId());
        withdrawalAuto.setCreationTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_AUTO + SecurityUtils.getPkCountry());
        return toAjax(withdrawalAutoService.save(withdrawalAuto));
    }


    /**
     * @description: 修改自动提现设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdWithdrawal]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdWithdrawalAuto withdrawalAuto) {
        withdrawalAuto.setPkModified(SecurityUtils.getUserId());
        withdrawalAuto.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_AUTO + SecurityUtils.getPkCountry());
        return toAjax(withdrawalAutoService.updateById(withdrawalAuto));
    }


    /**
     * @description: 查询单条自动提现设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdWithdrawalAuto withdrawalAuto = withdrawalAutoService.getById(pkId);
        return AjaxResult.success(withdrawalAuto);
    }


    /**
     * @description: 删除自动提现设置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdWithdrawalAuto> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_AUTO + SecurityUtils.getPkCountry());
        return toAjax(withdrawalAutoService.update(updateWrapper));
    }
}
