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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
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.exception.MyException;
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 com.fzm.mall.server.admin.shop.entity.dto.ShopDTO;
import com.fzm.mall.server.admin.shop.entity.dto.ShopSaveDTO;
import com.fzm.mall.server.admin.shop.entity.dto.ShopUpdateDTO;
import com.fzm.mall.server.admin.shop.service.IShopService;
import com.fzm.mall.server.admin.util.PhoneFormatCheckUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
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;

/**
 * <p>
 * 店铺中心-店铺 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-16
 */
@Api(tags = "店铺中心")
@RestController
@RequestMapping("/shop/shopPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopController {

    private final IShopService shopService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "店铺中心-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "店铺中心-新增", module = OperateModuleEnum.SHOP_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid ShopSaveDTO shopSaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        if(!PhoneFormatCheckUtils.isPhoneLegal(shopSaveDTO.getContactPhone())){
            throw new MyException(MallResponseError.PHONE_FORMAT__ERROR);
        }
        Admin admin = adminService.getByAdminId(adminId);
        shopSaveDTO.setMerchantId(admin.getMerchantId());
        shopSaveDTO.setAdminId(adminId);

        shopService.save(shopSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "店铺中心-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "店铺中心-编辑", module = OperateModuleEnum.SHOP_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid ShopUpdateDTO shopUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        if(!PhoneFormatCheckUtils.isPhoneLegal(shopUpdateDTO.getContactPhone())){
            throw new MyException(MallResponseError.PHONE_FORMAT__ERROR);
        }
        Admin admin = adminService.getByAdminId(adminId);
        shopUpdateDTO.setMerchantId(admin.getMerchantId());
        shopUpdateDTO.setAdminId(adminId);

        shopService.update(shopUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "店铺中心-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "店铺中心-删除", module = OperateModuleEnum.SHOP_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{shopId}")
    public ResponseVO delete(
            @ApiParam(value = "shopId", required = true) @PathVariable(value = "shopId") String shopId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        shopService.delete(shopId, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "店铺中心-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{shopId}")
    public ResponseVO<ShopDTO> read(
            @ApiParam(value = "shopId", required = true) @PathVariable(value = "shopId") String shopId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        ShopDTO result = shopService.read(shopId, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "店铺中心-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<ShopDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        Admin admin = adminService.getByAdminId(adminId);
        IPage<ShopDTO> result = shopService.page(page, admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }
}
