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


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.NavigationDTO;
import com.fzm.mall.server.admin.shop.entity.dto.NavigationSaveDTO;
import com.fzm.mall.server.admin.shop.entity.dto.NavigationUpdateDTO;
import com.fzm.mall.server.admin.shop.entity.dto.ShopDTO;
import com.fzm.mall.server.admin.shop.service.INavigationService;
import com.fzm.mall.server.admin.shop.service.IShopService;
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;
import java.util.List;

/**
 * <p>
 * 店铺中心-导航配置店铺导航管理 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-04-26
 */
@Api(tags = "店铺中心")
@RestController
@RequestMapping("/navigation/navigationPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NavigationController {

    private final INavigationService shopNavigationService;
    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 NavigationSaveDTO shopNavigationSaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        shopNavigationSaveDTO.setMerchantId(admin.getMerchantId());
        ShopDTO shopDTO = shopService.readByMerchantId(admin.getMerchantId());
        if(null == shopDTO){
            throw new MyException(MallResponseError.SHOP_NOT_EXIST);
        }
        shopNavigationSaveDTO.setShopId(shopDTO.getShopId());
        shopNavigationService.save(shopNavigationSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "店铺中心-导航配置编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "店铺中心-导航配置编辑", module = OperateModuleEnum.SHOP_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid NavigationUpdateDTO shopNavigationUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        shopNavigationUpdateDTO.setMerchantId(admin.getMerchantId());

        shopNavigationService.update(shopNavigationUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

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

    @ApiOperation(value = "店铺中心-导航配置列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {

        Admin admin = adminService.getByAdminId(adminId);
        List<NavigationDTO> result = shopNavigationService.listAll(admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }
}
