
package com.tmerclub.cloud.delivery.controller.platform;

import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.vo.AreaVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.delivery.constant.AreaLevel;
import com.tmerclub.cloud.common.order.dto.AreaDTO;
import com.tmerclub.cloud.delivery.model.Area;
import com.tmerclub.cloud.delivery.service.AreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 省市区地区信息
 *
 * @author YXF
 * @date 2020-11-25 14:48:52
 */
@RestController("platformAreaController")
@RequestMapping("/p/area")
@Tag(name = "店铺-地区信息")
public class AreaController {

    @Autowired
    private AreaService areaService;


    @PostMapping
    @Operation(summary = "保存省市区地区信息", description = "保存省市区地区信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody AreaDTO areaDTO) {
        Area area = BeanUtil.map(areaDTO, Area.class);
        area.setAreaId(null);
        // 查询相同上级的当前地区名称数量
        Integer count = areaService.countByNameAndParentId(areaDTO.getAreaName(), areaDTO.getLevel(), areaDTO.getParentId(), null);
        if (count > 0) {
            throw new LuckException("同一上级的当前地区名称已经存在");
        }
        areaService.save(area);
        areaService.removeAllCache(area.getParentId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新省市区地区信息", description = "更新省市区地区信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody AreaDTO areaDTO) {
        Area area = BeanUtil.map(areaDTO, Area.class);
        AreaVO dbArea = areaService.getByAreaId(area.getAreaId());
        // 判断当前省市区级别，如果是1级、2级则不能修改级别，不能修改成别人的下级
        if(Objects.equals(dbArea.getLevel(), AreaLevel.FIRST_LEVEL.value()) && !Objects.equals(area.getLevel(), AreaLevel.FIRST_LEVEL.value())){
            throw new LuckException("不能改变一级行政地区的级别");
        }
        if(Objects.equals(dbArea.getLevel(), AreaLevel.SECOND_LEVEL.value()) && !Objects.equals(area.getLevel(), AreaLevel.SECOND_LEVEL.value())){
            throw new LuckException("不能改变二级行政地区的级别");
        }
        // 查询相同上级的当前地区名称数量
        Integer count = areaService.countByNameAndParentId(areaDTO.getAreaName(), areaDTO.getLevel(), areaDTO.getParentId(), dbArea.getAreaId());
        if (count > 0) {
            throw new LuckException("同一上级的当前地区名称已经存在");
        }
        areaService.update(area);
        areaService.removeAllCache(dbArea.getParentId());
        areaService.removeAreaCacheByParentId(area.getParentId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除省市区地区信息", description = "根据省市区地区信息id删除省市区地区信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long areaId) {
        AreaVO dbArea = areaService.getByAreaId(areaId);
        areaService.deleteById(areaId);
        areaService.removeAllCache(dbArea.getParentId());
        return ServerResponseEntity.success();
    }
}
