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


import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.distribution.entity.dto.TemplateDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.TemplateSaveDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.TemplateUpdateDTO;
import com.fzm.mall.server.admin.distribution.service.ITemplateService;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
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.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-03-04
 */
@Api(tags = "商户-物流中心")
@RestController
@RequestMapping("/template/templatePermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TemplateController {

    private final ITemplateService templateService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "物流中心-运费模板-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "物流中心-运费模板-新增", module = OperateModuleEnum.DISTRIBUTION_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid TemplateSaveDTO templateSaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        templateSaveDTO.setMerchantId(admin.getMerchantId());

        templateService.save(templateSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "物流中心-运费模板-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "物流中心-运费模板-编辑", module = OperateModuleEnum.DISTRIBUTION_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid TemplateUpdateDTO templateUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        templateUpdateDTO.setMerchantId(admin.getMerchantId());

        templateService.update(templateUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "物流中心-运费模板-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "物流中心-运费模板-删除", module = OperateModuleEnum.DISTRIBUTION_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{templateId}")
    public ResponseVO delete(
            @ApiParam(value = "模板id", required = true) @PathVariable(value = "templateId") String templateId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        templateService.delete(templateId, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "物流中心-运费模板-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{templateId}")
    public ResponseVO<TemplateDTO> read(
            @ApiParam(value = "模板id", required = true) @PathVariable(value = "templateId") String templateId,
            @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        TemplateDTO result = templateService.read(templateId, 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<TemplateDTO> result = templateService.list(admin.getMerchantId());
        return resFac.getSuccessResponseWithData(result);
    }
}
