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

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.distribution.vo.DistributionSpuVO;
import com.tmerclub.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
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.distribution.constant.DistributionSpuStatus;
import com.tmerclub.cloud.distribution.dto.DistributionSpuDTO;
import com.tmerclub.cloud.distribution.service.DistributionSpuService;
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 cl
 * @date 2021-08-09 14:14:07
 */
@RestController("multishopDistributionSpuController")
@RequestMapping("/m/distributionSpu")
@Tag(name = "multishop-分销商品关联信息")
public class DistributionSpuController {

    @Autowired
    private DistributionSpuService distributionSpuService;

    @GetMapping
    @Operation(summary = "获取分销商品关联信息", description = "根据distributionSpuId获取分销商品关联信息")
    public ServerResponseEntity<DistributionSpuVO> getByDistributionSpuId(@RequestParam Long distributionSpuId) {
        return ServerResponseEntity.success(distributionSpuService.getByDistributionSpuId(distributionSpuId));
    }

    @PostMapping
    @Operation(summary = "保存分销商品关联信息", description = "保存分销商品关联信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody DistributionSpuDTO distributionSpuDTO) {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        distributionSpuDTO.setShopId(uidInfoBO.getTenantId());
        distributionSpuDTO.setModifier(uidInfoBO.getUserId());
        distributionSpuService.save(distributionSpuDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新分销商品关联信息", description = "更新分销商品关联信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionSpuDTO distributionSpuDTO) {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        distributionSpuDTO.setShopId(uidInfoBO.getTenantId());
        distributionSpuDTO.setModifier(uidInfoBO.getUserId());
        distributionSpuDTO.setState(null);
        distributionSpuService.update(distributionSpuDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除分销商品关联信息", description = "根据分销商品关联信息id删除分销商品关联信息")
    public ServerResponseEntity<Void> delete(@RequestParam Long distributionSpuId) {
        Long shopId = AuthUserContext.get().getTenantId();
        distributionSpuService.deleteByIdAndShopId(distributionSpuId, shopId);
        return ServerResponseEntity.success();
    }


    @GetMapping("/getOfflineHandleEvent")
    @Operation(summary = "获取分销商品下线信息", description = "获取分销商品下线信息")
    public ServerResponseEntity<OfflineHandleEventVO> getOfflineHandleEvent(@RequestParam("distributionSpuId") Long distributionSpuId) {
        return ServerResponseEntity.success(distributionSpuService.getOfflineHandleEvent(distributionSpuId));
    }

    @PostMapping("/auditApply")
    @Operation(summary = "违规分销商品提交重新上线申请", description = "违规分销商品提交重新上线申请")
    public ServerResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventDTO offlineHandleEventDTO) {
        distributionSpuService.auditApply(offlineHandleEventDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/updateState")
    @Operation(summary = "上架或下架分销商品", description = "上架或下架分销商品")
    public ServerResponseEntity<Void> updateState(@RequestParam("distributionSpuId") Long distributionSpuId, @RequestParam("state") Integer state) {
        if (!Objects.equals(DistributionSpuStatus.OFF_SHELF.value(), state) && !Objects.equals(DistributionSpuStatus.PUT_SHELF.value(), state)) {
            throw new LuckException("状态错误");
        }
        distributionSpuService.updateState(distributionSpuId, state);
        return ServerResponseEntity.success();
    }
}
