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.AddressDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.AddressSaveDTO;
import com.fzm.mall.server.admin.distribution.entity.dto.AddressUpdateDTO;
import com.fzm.mall.server.admin.distribution.service.IAddressService;
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("/address/addressPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AddressController {

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

        addressService.save(addressSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

        addressService.update(addressUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

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

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