
package com.tmerclub.cloud.product.controller.admin;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.dto.StockChangeReasonDTO;
import com.tmerclub.cloud.product.model.StockChangeReason;
import com.tmerclub.cloud.product.model.StockChangeReasonLang;
import com.tmerclub.cloud.product.service.StockChangeReasonLangService;
import com.tmerclub.cloud.product.service.StockChangeReasonService;
import com.tmerclub.cloud.product.vo.StockChangeReasonVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
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.List;

/**
 * 出入库原因
 *
 * @author lhd
 * @date 2022-06-22 17:14:22
 */
@RestController("adminStockChangeReasonController")
@RequestMapping("/mp/stock_change_reason")
@Tag(name = "出入库原因")
public class StockChangeReasonController {

    @Autowired
    private StockChangeReasonService stockChangeReasonService;


    @Autowired
    private StockChangeReasonLangService stockChangeReasonLangService;

    @GetMapping("/page")
    @Operation(summary = "分页获取出入库原因列表", description = "分页获取出入库原因列表")
    public ServerResponseEntity<PageVO<StockChangeReasonVO>> page(@Valid PageDTO pageDTO, StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        stockChangeReasonDTO.setSysType(AuthUserContext.get().getSysType());
        PageVO<StockChangeReasonVO> stockChangeReasonPage = stockChangeReasonService.page(pageDTO, stockChangeReasonDTO);
        return ServerResponseEntity.success(stockChangeReasonPage);
    }

    @GetMapping("/list")
    @Operation(summary = "获取店铺所有的出入库原因", description = "获取店铺所有的出入库原因")
    public ServerResponseEntity<List<StockChangeReasonVO>> list(StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        stockChangeReasonDTO.setSysType(AuthUserContext.get().getSysType());
        List<StockChangeReasonVO> stockChangeReasonPage = stockChangeReasonService.list(stockChangeReasonDTO);
        return ServerResponseEntity.success(stockChangeReasonPage);
    }

    @GetMapping
    @Operation(summary = "获取出入库原因", description = "根据stockChangeReasonId获取出入库原因")
    public ServerResponseEntity<StockChangeReasonVO> getByStockChangeReasonId(@RequestParam Long stockChangeReasonId) {
        return ServerResponseEntity.success(stockChangeReasonService.getByStockChangeReasonId(stockChangeReasonId));
    }

    @PostMapping
    @Operation(summary = "保存出入库原因", description = "保存出入库原因")
    public ServerResponseEntity<Void> save(@Valid @RequestBody StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonService.save(stockChangeReasonDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新出入库原因", description = "更新出入库原因")
    public ServerResponseEntity<Void> update(@Valid @RequestBody StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        stockChangeReasonDTO.setSysType(AuthUserContext.get().getSysType());
        StockChangeReason stockChangeReason = BeanUtil.map(stockChangeReasonDTO, StockChangeReason.class);
        stockChangeReasonService.update(stockChangeReason);
        //更新出入库原因和备注信息
        stockChangeReasonLangService.update(BeanUtil.map(stockChangeReasonDTO, StockChangeReasonLang.class));
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除出入库原因", description = "根据出入库原因id删除出入库原因")
    public ServerResponseEntity<Void> delete(@RequestParam Long stockChangeReasonId) {
        stockChangeReasonService.deleteById(stockChangeReasonId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/change_status")
    @Schema(description = "修改出入库原因状态")
    public ServerResponseEntity<Void> changeStatus(@RequestParam("stockChangeReasonId") Long stockChangeReasonId, @RequestParam("status") Integer status) {
        Long shopId = AuthUserContext.get().getTenantId();
        stockChangeReasonService.changeStatus(shopId, stockChangeReasonId, status);
        return ServerResponseEntity.success();
    }
}
