package com.iformall.controller;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBuser;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.domain.po.WxProfitSharingReceiver;
import com.iformall.enums.EnumMsgSend;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxProfitSharingReceiverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/merchant")
@Api(description = "商户相关接口")
@Slf4j
public class WxMerchantController extends BaseController {

    @Autowired
    private WxMerchantService wxMerchantService;

    @Autowired
    private WxProfitSharingReceiverService wxProfitSharingReceiverService;

    @ApiOperation("查询当前租户下商户名称列表")
    @GetMapping("/name_list")
    public ResultData nameList() {
        log.debug("[" + getIpAddr() + "] WxMerchantController::name_list");
        return new ResultData(wxMerchantService.findList(getTenantId()));
    }

    @ApiOperation("查询账户信息")
    @GetMapping("/findAccountById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findAccountById(@ModelAttribute WxMerchant wxMerchant) {
        log.debug("[" + getIpAddr() + "] WxMerchantController::findAccountById");
        if (wxMerchant.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户ID不能为空");
        }
        wxMerchant.setTenantId(getTenantId());
        return new ResultData(wxProfitSharingReceiverService.findReceiver(wxMerchant));
    }

    @ApiOperation("更新收款账户信息:必填项 id(商户ID) name(商户名称) accountId(账号ID) accountTypeValue(账号类型) accountName(账号名称)")
    @PostMapping("/updateAccount")
    public ResultData addAccount(@RequestBody WxMerchant wxMerchant) {
        log.debug("[" + getIpAddr() + "] merchantProfitSharingReceiver::addAccount");
        if (!hasPermission()) {
            return new ResultData(ErrorCode.USER_NO_PERMISSION);
        }
        if (wxMerchant.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户ID不能为空");
        }
        if (wxMerchant.getName() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户名称不能为空");
        }
        if (wxMerchant.getAccountTypeValue() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "账号类型不能为空");
        }
        if (StringUtils.isEmpty(wxMerchant.getAccountId())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "账号ID不能为空");
        }
        if (StringUtils.isEmpty(wxMerchant.getAccountName())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "账号名称不能为空");
        }
        wxMerchant.setTenantId(getTenantId());
        return wxMerchantService.updateMerchantAccount(wxMerchant);
    }

    @ApiOperation("删除分账账户")
    @GetMapping("/deleteByReceiverId")
    @ApiImplicitParam(name = "id", value = "商户分账账户ID", dataType = "Long", paramType = "query", required = true)
    public ResultData deleteByReceiverId(@ModelAttribute WxProfitSharingReceiver receiver) {
        log.debug("[" + getIpAddr() + "] WxMerchantController::deleteByReceiverId");
        if (!hasPermission()) {
            return new ResultData(ErrorCode.USER_NO_PERMISSION);
        }
        if (receiver.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "商户分账账户ID不能为空");
        }
        receiver.setTenantId(getTenantId());
        return wxProfitSharingReceiverService.delReceiver(receiver, EnumMsgSend.MSG_SEND_IMMEDIATELY.getCode());
    }

    @ApiOperation("查询是否有权限修改账户")
    @GetMapping("/permitModifiy")
    public ResultData permitModifiy() {
        log.debug("[" + getIpAddr() + "] WxMerchantController::permitModifiy");
        boolean has = hasPermission();
        return new ResultData(has);
    }

    public boolean hasPermission() {
        WxMerchantBUser user = getUser();
        WxBuser buser = getBuser();
        WxMerchant merchant = new WxMerchant();
        merchant.setId(user.getMerchantId());
        WxMerchant wxMerchant = wxMerchantService.findOne(merchant);
        return wxMerchant != null && buser.getPhone().equals(wxMerchant.getLinkPhone());
    }

}
