package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.AccountInfo;
import com.fuint.common.dto.RaffleTypeDto;
import com.fuint.common.enums.StatusEnum;
import com.fuint.common.param.RaffleTypePage;
import com.fuint.common.service.RaffleTypeService;
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.repository.model.MtRaffleType;
import com.fuint.repository.model.MtStore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
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/raffleType")
public class BackendRaffleTypeController extends BaseController {

    /**
     * 抽奖类型服务接口
     */
    private RaffleTypeService raffleTypeService;

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

    /**
     * 抽奖类型列表查询
     */
    @ApiOperation(value = "抽奖类型列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject list(@ModelAttribute RaffleTypePage page) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
            page.setMerchantId(accountInfo.getMerchantId());
        }
        if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
            page.setStoreId(accountInfo.getStoreId());
        }

        PaginationResponse<MtRaffleType> paginationResponse = raffleTypeService.queryRaffleTypeListByPagination(page);
        List<MtStore> storeList = storeService.getMyStoreList(accountInfo.getMerchantId(), accountInfo.getStoreId(), StatusEnum.ENABLED.getKey());

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

        return getSuccessResult(result);
    }

    /**
     * 更新抽奖类型状态
     */
    @ApiOperation(value = "更新抽奖类型状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject updateStatus(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
        Integer typeId = params.get("typeId") == null ? 0 : Integer.parseInt(params.get("typeId").toString());

        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        MtRaffleType raffleType = raffleTypeService.getRaffleTypeById(typeId);
        if (raffleType == null) {
            return getFailureResult(201);
        }

        raffleType.setOperator(accountInfo.getAccountName());
        raffleType.setStatus(status);
        raffleTypeService.updateRaffleType(raffleType);

        return getSuccessResult(true);
    }

    /**
     * 保存抽奖类型
     */
    @ApiOperation(value = "保存抽奖类型")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseObject saveHandler(@RequestBody RaffleTypeDto raffleTypeDto) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        if (accountInfo.getMerchantId() == null || accountInfo.getMerchantId() < 1) {
            return getFailureResult(5002);
        }

        MtRaffleType raffleType = new MtRaffleType();
        BeanUtils.copyProperties(raffleTypeDto, raffleType);
        raffleType.setOperator(accountInfo.getAccountName());
        raffleType.setMerchantId(accountInfo.getMerchantId());
        if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
            raffleType.setStoreId(accountInfo.getStoreId());
        }
        if (raffleTypeDto.getId() != null && raffleTypeDto.getId() > 0) {
            raffleTypeService.updateRaffleType(raffleType);
        } else {
            raffleTypeService.addRaffleType(raffleType);
        }

        return getSuccessResult(true);
    }

    /**
     * 获取抽奖类型详情
     */
    @ApiOperation(value = "获取抽奖类型详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject info(@PathVariable("id") Integer id) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        MtRaffleType raffleType = raffleTypeService.getRaffleTypeById(id);

        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
            if (raffleType != null && !raffleType.getMerchantId().equals(accountInfo.getMerchantId())) {
                return getFailureResult(1004);
            }
        }

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

        return getSuccessResult(result);
    }
}
