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


import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.distribution.entity.dto.CompanyDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.CompanySaveDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.CompanyUpdateDTO;
import com.fzm.mall.server.admin.distribution.service.ICompanyService;
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.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;
import java.util.List;

/**
 * <p>
 * 物流中心-快递公司 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-03
 */
@Api(tags = "商户-物流中心")
@RestController
@RequestMapping("/company/companyPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CompanyController {

    private final ICompanyService companyService;
    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 CompanySaveDTO companySaveDTO,
                           @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        if(!PhoneFormatCheckUtils.isPhoneLegal(companySaveDTO.getNetworkPhone())){
            throw new MyException(MallResponseError.PHONE_FORMAT__ERROR);
        }
        Admin admin = adminService.getByAdminId(adminId);
        companySaveDTO.setMerchantId(admin.getMerchantId());

        companyService.save(companySaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "物流中心-快递公司-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "物流中心-快递公司-编辑", module = OperateModuleEnum.DISTRIBUTION_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid CompanyUpdateDTO companyUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        if(!PhoneFormatCheckUtils.isPhoneLegal(companyUpdateDTO.getNetworkPhone())){
            throw new MyException(MallResponseError.PHONE_FORMAT__ERROR);
        }
        Admin admin = adminService.getByAdminId(adminId);
        companyUpdateDTO.setMerchantId(admin.getMerchantId());

        companyService.update(companyUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

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