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

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.tmerclub.cloud.api.delivery.vo.SameCityVO;
import com.tmerclub.cloud.common.bean.BmapPoint;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.delivery.dto.SameCityDTO;
import com.tmerclub.cloud.delivery.model.SameCity;
import com.tmerclub.cloud.delivery.service.SameCityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author lhd
 * @date 2020-05-23 08:26:25
 */
@RestController("multishopSameCityController")
@RequestMapping("/m/sameCity")
@Tag(name = "商家端同城配送接口")
public class SameCityController {

    @Autowired
    private SameCityService sameCityService;

    @GetMapping("/getSameCityPage")
    @Operation(summary = "获取同城配送数据")
    public ServerResponseEntity<SameCityVO> getSameCityPage() {
        Long shopId = AuthUserContext.get().getTenantId();
        SameCity sameCity = sameCityService.getSameCityByShopId(shopId);
        if (sameCity == null) {
            return ServerResponseEntity.success(null);
        }
        SameCityVO sameCityVO = new SameCityVO();
        BeanUtils.copyProperties(sameCity, sameCityVO);
        sameCityVO.setPolygonPath(JSON.parseArray(sameCity.getPositionInfo(), BmapPoint.class));
        return ServerResponseEntity.success(sameCityVO);
    }

    @PostMapping
    @Operation(summary = "新增或修改同城配送数据")
    public ServerResponseEntity<Boolean> saveOrUpdate(@RequestBody @Valid SameCityDTO sameCityDTO) {
        if (CollUtil.isEmpty(sameCityDTO.getPolygonPath())) {
            throw new LuckException("请设置设置配送区域");
        }
        Long shopId = AuthUserContext.get().getTenantId();
        SameCity sameCity = sameCityService.getSameCityByShopId(shopId);
        String positionInfo = JSON.toJSONString(sameCityDTO.getPolygonPath());
        boolean isAdd = false;
        if (sameCity == null) {
            sameCity = new SameCity();
            isAdd = true;
        }
        BeanUtils.copyProperties(sameCityDTO, sameCity);
        sameCity.setPositionInfo(positionInfo);
        sameCity.setShopId(shopId);
        // 新增or更新
        if (isAdd) {
            sameCityService.save(sameCity);
        } else {
            sameCityService.updateByShopId(sameCity);
        }
        // 删除缓存
        sameCityService.removeSameCityCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改同城配送数据")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SameCityDTO sameCityDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        // 更新同城配送
        SameCity sameCity = new SameCity();
        BeanUtils.copyProperties(sameCityDTO, sameCity);
        sameCity.setShopId(shopId);
        sameCity.setPositionInfo(JSON.toJSONString(sameCityDTO.getPolygonPath()));
        sameCityService.updateByShopId(sameCity);
        // 删除缓存
        sameCityService.removeSameCityCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }
}
