package com.fzm.mall.server.admin.goods_center.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.goods_center.entity.dto.GoodsEditDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.OnOffDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.GoodsSpuDetailVO;
import com.fzm.mall.server.admin.goods_center.entity.vo.GoodsSpuVO;
import com.fzm.mall.server.admin.goods_center.enums.status.SpuStatusEnum;
import com.fzm.mall.server.admin.goods_center.service.ISpuService;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangp
 * @date 2021/5/27 10:27
 * @description 商品列表
 * @since JDK 1.8
 */
@Api(tags = "商品中心-商品列表")
@RestController
@RequestMapping("/goodsList")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsListController {

    private final IAdminService adminService;
    private final ISpuService spuService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ResponseVO page(
            @ApiParam(name = "goodsId", value = "商品编号")
            @RequestParam(required = false) String goodsId,
            @ApiParam(name = "goodsName", value = "商品名称")
            @RequestParam(required = false) String goodsName,
            @ApiParam(name = "status", value = "状态")
            @RequestParam(required = false) Integer status,
            @ApiParam(name = "addTimeHead", value = "开始时间")
            @RequestParam(required = false) Long addTimeHead,
            @ApiParam(name = "addTimeTail", value = "结束时间")
            @RequestParam(required = false) Long addTimeTail,
            @ApiParam(name = "goodsType", value = "商品类型")
            @RequestParam(required = false) Integer goodsType,
            @ApiParam(name = "pageNum", value = "当前页数")
            @RequestParam Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页个数")
            @RequestParam Integer pageSize,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        IPage<GoodsSpuVO> page = spuService.page(admin.getMerchantId(), admin.getType(), goodsId, goodsName, status,
                addTimeHead, addTimeTail, goodsType, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation(value = "批量删除")
    @AdminLog(description = "商品中心-批量删除", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.DEFAULT_FIRST)
    @DeleteMapping("/deleteBatch")
    public ResponseVO deleteBatch(@RequestBody List<String> goodsIds, @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        String merchantId = adminService.getByAdminId(adminId).getMerchantId();
        spuService.deleteBatch(goodsIds, merchantId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/detail")
    public ResponseVO detail(@ApiParam(name = "goodsId", value = "商品id") @RequestParam String goodsId) {
        GoodsSpuDetailVO detailVO = spuService.getVoByGoodsId(goodsId);
        return resFac.getSuccessResponseWithData(detailVO);
    }

    //TODO 商户下架联动

    @ApiOperation(value = "上下架")
    @AdminLog(description = "商品中心-上下架", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.ON_OFF)
    @PutMapping("/onOff")
    public ResponseVO onOff(@RequestBody OnOffDTO onOffDTO, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        String goodsId = onOffDTO.getGoodsId();
        Integer type = onOffDTO.getType();
        String merchantId1 = adminService.getByAdminId(adminId).getMerchantId();
        String merchantId2 = spuService.getByGoodsId(goodsId).getMerchantId();
        if (merchantId1.equals(MALLGlobalConfig.PLATFORM_MERCHANT_ID)) {
            if (type.equals(SpuStatusEnum.ON.getStatus())) {
                return resFac.getResponse(MallResponseError.NO_AUTH_ERROR);
            }
        } else {
            if (!merchantId1.equals(merchantId2)) {
                return resFac.getResponse(MallResponseError.NO_AUTH_ERROR);
            }
        }
        spuService.offGoods(goodsId, type);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "编辑")
    @AdminLog(description = "商品中心-编辑商品", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.EDIT_GOODS)
    @PutMapping("/edit")
    public ResponseVO edit(@RequestBody @Valid GoodsEditDTO eDTO, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getMerchantTypeArr());
        String merchantId = adminService.getByAdminId(adminId).getMerchantId();
        spuService.edit(eDTO, merchantId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "取消审核")
    @AdminLog(description = "商品中心-取消审核", module = OperateModuleEnum.GOODS_CENTER, opType = OperateTypeEnum.DEFAULT_FIRST)
    @PutMapping("/cancelCheck/{goodsId}")
    public ResponseVO cancelCheck(@PathVariable String goodsId, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthCheck(adminId, AdminTypeEnum.MERCHANT.getType());
        String merchantId1 = adminService.getByAdminId(adminId).getMerchantId();
        String merchantId2 = spuService.getByGoodsId(goodsId).getMerchantId();
        if (!merchantId1.equals(merchantId2)) {
            return resFac.getResponse(MallResponseError.NO_AUTH_ERROR);
        }
        spuService.cancel(goodsId);
        return resFac.getSimpleSuccessResponse();
    }

}
