package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.CommissionLogDto;
import com.fuint.common.dto.ParamDto;
import com.fuint.common.enums.CommissionStatusEnum;
import com.fuint.common.enums.CommissionTargetEnum;
import com.fuint.common.enums.StatusEnum;
import com.fuint.common.param.CommissionLogPage;
import com.fuint.common.service.CommissionCashService;
import com.fuint.common.service.CommissionLogService;
import com.fuint.common.service.StoreService;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.module.backendApi.request.CommissionLogRequest;
import com.fuint.module.backendApi.request.CommissionSettleRequest;
import com.fuint.repository.model.MtStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分销提成记录管理类controller
 *
 * Created by FSQ
 * CopyRight https://www.fuint.cn
 */
@Api(tags="管理端-分销提成记录相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/commissionLog")
public class BackendCommissionLogController extends BaseController {

    /**
     * 分销提成记录业务接口
     */
    private CommissionLogService commissionLogService;

    /**
     * 分销提成提现业务接口
     */
    private CommissionCashService commissionCashService;

    /**
     * 店铺服务接口
     */
    private StoreService storeService;

    /**
     * 分销提成记录列表查询
     */
    @ApiOperation(value = "分销提成记录查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('commission:log:index')")
    public ResponseObject list(@ModelAttribute CommissionLogPage commissionLogPage) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
            commissionLogPage.setStoreId(accountInfo.getStoreId());
        }
        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
            commissionLogPage.setMerchantId(accountInfo.getMerchantId());
        }

        PaginationResponse<CommissionLogDto> paginationResponse = commissionLogService.queryCommissionLogByPagination(commissionLogPage);

        // 店铺列表
        List<MtStore> storeList = storeService.getMyStoreList(accountInfo.getMerchantId(), accountInfo.getStoreId(), StatusEnum.ENABLED.getKey());

        // 状态列表
        List<ParamDto> statusList = CommissionStatusEnum.getCommissionStatusList();

        // 分佣对象列表
        List<ParamDto> targetList = CommissionTargetEnum.getCommissionTargetList();

        Map<String, Object> result = new HashMap<>();
        result.put("dataList", paginationResponse);
        result.put("storeList", storeList);
        result.put("statusList", statusList);
        result.put("targetList", targetList);

        return getSuccessResult(result);
    }

    /**
     * 获取分销提成记录详情
     */
    @ApiOperation(value = "获取分销提成记录详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('commission:log:index')")
    public ResponseObject info(@PathVariable("id") Integer id) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();

        CommissionLogDto commissionLog = commissionLogService.queryCommissionLogById(id);
        if (accountInfo.getMerchantId() > 0 && !commissionLog.getMerchantId().equals(accountInfo.getMerchantId())) {
            return getFailureResult(1004);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("commissionLog", commissionLog);

        return getSuccessResult(result);
    }

    /**
     * 修改分销提成记录
     */
    @ApiOperation(value = "修改分销提成记录")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("@pms.hasPermission('commission:log:index')")
    public ResponseObject save(@RequestBody CommissionLogRequest commissionLogRequest) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();

        commissionLogRequest.setOperator(accountInfo.getAccountName());
        commissionLogService.updateCommissionLog(commissionLogRequest);

        return getSuccessResult(true);
    }

    /**
     * 作废分销提成记录
     */
    @ApiOperation(value = "作废分销提成记录")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('commission:log:index')")
    public ResponseObject delete(@PathVariable("id") Integer id) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();

        CommissionLogDto commissionLog = commissionLogService.queryCommissionLogById(id);
        if (accountInfo.getMerchantId() > 0 && !commissionLog.getMerchantId().equals(accountInfo.getMerchantId())) {
            return getFailureResult(1004);
        }

        CommissionLogRequest commissionLogRequest = new CommissionLogRequest();
        commissionLogRequest.setId(id);
        commissionLogRequest.setStatus(CommissionStatusEnum.CANCEL.getKey());
        commissionLogService.updateCommissionLog(commissionLogRequest);

        return getSuccessResult(true);
    }

    /**
     * 分销提成结算
     */
    @ApiOperation(value = "分销提成结算")
    @RequestMapping(value = "/doSettle", method = RequestMethod.POST)
    @PreAuthorize("@pms.hasPermission('commission:log:index')")
    public ResponseObject doSettle(@RequestBody CommissionSettleRequest commissionSettleRequest) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();

        commissionSettleRequest.setOperator(accountInfo.getAccountName());
        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
            commissionSettleRequest.setMerchantId(accountInfo.getMerchantId());
        }
        if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
            commissionSettleRequest.setStoreId(accountInfo.getStoreId());
        }
        String settleNo = commissionCashService.settleCommission(commissionSettleRequest);
        return getSuccessResult(settleNo);
    }
}
