package com.slodon.b2b2c.controller.member.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.member.example.MemberApplyCashExample;
import com.slodon.b2b2c.member.example.MemberExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberApplyCash;
import com.slodon.b2b2c.model.member.MemberApplyCashModel;
import com.slodon.b2b2c.model.member.MemberModel;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.vo.member.MemberApplyCashVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author slodon
 */
@Api(tags = "admin-会员提现管理")
@RestController
@RequestMapping("v3/member/admin/member/withdraw")
public class AdminMemberWithdrawController extends BaseController {

    @Resource
    private MemberModel memberModel;
    @Resource
    private MemberApplyCashModel memberApplyCashModel;

    @ApiOperation("会员提现列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cashSn", value = "提现单号", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "申请开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "申请结束时间", paramType = "query"),
            @ApiImplicitParam(name = "memberName", value = "会员名", paramType = "query"),
            @ApiImplicitParam(name = "receiveName", value = "收款人", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态：1、待处理；2、提现成功；3、已拒绝；4、提现失败", paramType = "query"),
            @ApiImplicitParam(name = "receiveType", value = "收款方式：WXPAY-微信、ALIPAY-支付宝", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<MemberApplyCashVO>> getList(HttpServletRequest request, String cashSn, Date startTime, Date endTime,
                                                         String memberName, String receiveName, Integer state, String receiveType) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        MemberApplyCashExample example = new MemberApplyCashExample();
        example.setCashSnLike(cashSn);
        example.setApplyTimeAfter(startTime);
        example.setApplyTimeBefore(endTime);
        example.setMemberNameLike(memberName);
        example.setReceiveNameLike(receiveName);
        example.setState(state);
        example.setReceiveType(receiveType);
        List<MemberApplyCash> list = memberApplyCashModel.getMemberApplyCashList(example, pager);
        List<MemberApplyCashVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            //拼接会员id
            StringBuilder memberIds = new StringBuilder();
            //key-会员id，value-手机号
            Map<Integer, String> map = new HashMap<>(16);
            list.forEach(memberApplyCash -> {
                memberIds.append(",").append(memberApplyCash.getMemberId());
            });
            //查询会员信息
            MemberExample memberExample = new MemberExample();
            memberExample.setMemberIdIn(memberIds.substring(1));
            List<Member> memberList = memberModel.getMemberList(memberExample, null);
            memberList.forEach(member -> {
                if (!map.containsKey(member.getMemberId())) {
                    map.put(member.getMemberId(), member.getMemberMobile());
                }
            });
            list.forEach(memberApplyCash -> {
                MemberApplyCashVO vo = new MemberApplyCashVO(memberApplyCash);
                vo.setMemberMobile(map.getOrDefault(memberApplyCash.getMemberId(), null));
                vos.add(vo);
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("提现审核")
    @OperationLogger(option = "提现审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cashIds", value = "提现id集合，用英文逗号隔开", required = true),
            @ApiImplicitParam(name = "isPass", value = "是否审核通过：true-是,false-否", required = true),
            @ApiImplicitParam(name = "refuseReason", value = "拒绝理由,拒绝时必填")
    })
    @PostMapping("audit")
    public JsonResult<Object> withdrawAudit(HttpServletRequest request, String cashIds, Boolean isPass, String refuseReason) {
        Admin admin = UserUtil.getUser(request, Admin.class);
        //参数校验
        AssertUtil.notEmpty(cashIds, "请选择要审核的数据");
        AssertUtil.notFormatFrontIds(cashIds, "cashIds格式错误,请重试");

        String ip = WebUtil.getRealIp(request);
        memberApplyCashModel.withdrawAudit(admin, cashIds, isPass, refuseReason, ip);
        return SldResponse.success(isPass ? "审核通过" : "审核拒绝");
    }
}
