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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.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;

/**
 * 提现设置特殊配置 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/withdrawal-special")
public class BdWithdrawalSpecialController extends BaseController {

    @Autowired
    private IBdWithdrawalSpecialService iBdWithdrawalSpecialService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 查询提现设置特殊配置列表
     **/
    @GetMapping("/list")
    public TableDataInfo list(BdWithdrawalSpecial withdrawalSpecial) {
        startPage();
        LambdaQueryWrapper<BdWithdrawalSpecial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdWithdrawalSpecial::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(withdrawalSpecial.getPkWithdrawal())) {
            queryWrapper.eq(BdWithdrawalSpecial::getPkWithdrawal, withdrawalSpecial.getPkWithdrawal());
        }
        queryWrapper.orderByDesc(BdWithdrawalSpecial::getCreationTime);
        List<BdWithdrawalSpecial> list = iBdWithdrawalSpecialService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增提现设置特殊配置
     **/
    @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());
        return toAjax(iBdWithdrawalSpecialService.save(withdrawalSpecial));
    }


    /**
     * 修改提现设置特殊配置
     **/
    @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());
        return toAjax(iBdWithdrawalSpecialService.updateById(withdrawalSpecial));
    }


    /**
     * 查询单条提现设置特殊配置
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdWithdrawalSpecial withdrawalSpecial = iBdWithdrawalSpecialService.getById(pkId);
        return AjaxResult.success(withdrawalSpecial);
    }


    /**
     * 删除提现设置特殊配置
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<BdWithdrawalSpecial> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdWithdrawalSpecial::getPkId, pkId);
        updateWrapper.set(BdWithdrawalSpecial::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdWithdrawalSpecial::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdWithdrawalSpecial::getModifiedTime, new Date());
        return toAjax(iBdWithdrawalSpecialService.update(updateWrapper));
    }
}
