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.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
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.member.base.CuMember;
import com.hzs.common.domain.system.config.BdWithdrawalSpecial;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.base.dto.LoginMember;
import com.hzs.system.config.service.IBdWithdrawalSpecialService;
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.Date;
import java.util.List;

/**
 * <p>
 * 提现设置特殊配置 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-06
 */
@Slf4j
@RestController
@RequestMapping("/manage/withdrawal-special")
public class BdWithdrawalSpecialController extends BaseController {

    @Autowired
    private IBdWithdrawalSpecialService withdrawalSpecialService;
    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    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(BdWithdrawalSpecial withdrawalSpecial) {
        startPage();
        QueryWrapper<BdWithdrawalSpecial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(withdrawalSpecial.getPkWithdrawal())) {
            queryWrapper.eq("PK_WITHDRAWAL", withdrawalSpecial.getPkWithdrawal());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdWithdrawalSpecial> list = withdrawalSpecialService.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 BdWithdrawalSpecial withdrawalSpecial) {
        if (StringUtils.isEmpty(withdrawalSpecial.getMemberCode())) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_CODE_NOT_NULL);
        }
        withdrawalSpecial.setMemberCode(withdrawalSpecial.getMemberCode());
        R<CuMember> member = iMemberServiceApi.getMember(withdrawalSpecial.getMemberCode());
        if (StringUtils.isNull(member.getData())) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_NOT_VALID);
        }
        withdrawalSpecial.setPkCountry(SecurityUtils.getPkCountry());
        withdrawalSpecial.setPkCreator(SecurityUtils.getUserId());
        withdrawalSpecial.setCreationTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(withdrawalSpecialService.save(withdrawalSpecial));
    }


    /**
     * @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 BdWithdrawalSpecial withdrawalSpecial) {
        if (StringUtils.isEmpty(withdrawalSpecial.getMemberCode())) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_CODE_NOT_NULL);
        }
        withdrawalSpecial.setMemberCode(withdrawalSpecial.getMemberCode());
        R<LoginMember> member = iMemberServiceApi.getMemberInfo(withdrawalSpecial.getMemberCode());
        if (StringUtils.isNull(member.getData())) {
            return AjaxResult.error(ConfigMsgConstants.MEMBER_NOT_VALID);
        }
        withdrawalSpecial.setPkModified(SecurityUtils.getUserId());
        withdrawalSpecial.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_WITHDRAWAL_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(withdrawalSpecialService.updateById(withdrawalSpecial));
    }


    /**
     * @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) {
        BdWithdrawalSpecial withdrawalSpecial = withdrawalSpecialService.getById(pkId);
        return AjaxResult.success(withdrawalSpecial);
    }


    /**
     * @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<BdWithdrawalSpecial> 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_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(withdrawalSpecialService.update(updateWrapper));
    }
}
