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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.constant.TakeStockBillStatus;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.dto.TakeStockDTO;
import com.tmerclub.cloud.product.model.TakeStock;
import com.tmerclub.cloud.product.service.TakeStockService;
import com.tmerclub.cloud.product.vo.TakeStockVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author lhd
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopTakeStockController")
@RequestMapping("/m/take_stock")
@Tag(name = "实物盘点接口")
public class TakeStockController {

    @Autowired
    private TakeStockService takeStockService;
    @DubboReference
    private AccountFeignClient accountFeignClient;

    @GetMapping("/page")
    @Operation(summary = "分页查询库存盘点信息", description = "分页查询库存盘点信息")
    public ServerResponseEntity<PageVO<TakeStockVO>> getTakeStockPage(PageDTO pageDTO, TakeStockDTO takeStockDTO) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<TakeStockVO> tackStockPage = takeStockService.pageByParam(pageDTO, takeStockDTO);
        return ServerResponseEntity.success(tackStockPage);
    }

    @GetMapping("/info/{takeStockId}")
    @Operation(summary = "根据id查询库存盘点信息", description = "根据id查询库存盘点信息")
    public ServerResponseEntity<TakeStockVO> getById(@PathVariable("takeStockId") Long takeStockId) {
        return ServerResponseEntity.success(takeStockService.getByTakeStockId(takeStockId));
    }

    @PostMapping
    @Operation(summary = "新增盘点", description = "新增盘点")
    public ServerResponseEntity<Long> save(@RequestBody @Valid TakeStock takeStock) {
        if (Objects.isNull(takeStock.getCreateTime())) {
            throw new LuckException("盘点开始时间不能为空");
        }
        UidInfoBO userInfo = AuthUserContext.get();
        AuthAccountVO accountVO = accountFeignClient.getAccountInfoByTenantId(userInfo.getTenantId(), userInfo.getSysType()).getData();
        takeStock.setTakeStockNo(StringUtils.join("TSC", String.valueOf(new Snowflake().nextId())));
        takeStock.setShopId(userInfo.getTenantId());
        takeStock.setBillStatus(TakeStockBillStatus.TAKING.value());
        takeStock.setMaker(accountVO.getUserId());
        takeStock.setMakerMobile(accountVO.getPhone());
        takeStockService.save(takeStock);
        return ServerResponseEntity.success(takeStock.getTakeStockId());
    }

    @PutMapping
    @Operation(summary = "保存草稿", description = "保存草稿")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid TakeStockDTO takeStockDTO) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        if (CollectionUtil.isEmpty(takeStockDTO.getTakeStockSpuList())) {
            throw new LuckException("盘点商品不能为空");
        }
        takeStockService.updateTakeStock(takeStockDTO);
        return ServerResponseEntity.success(Boolean.TRUE);
    }

    @PutMapping("/finish_inventory")
    @Operation(summary = "完成盘点", description = "完成盘点")
    public ServerResponseEntity<Boolean> finishInventory(@RequestBody @Valid TakeStockDTO takeStockDTO) {
        UidInfoBO userInfo = AuthUserContext.get();
        AuthAccountVO accountVO = accountFeignClient.getAccountInfoByTenantId(userInfo.getTenantId(), userInfo.getSysType()).getData();
        takeStockDTO.setShopId(userInfo.getTenantId());
        takeStockDTO.setMaker(accountVO.getUserId());
        takeStockDTO.setMakerMobile(accountVO.getPhone());
        if (CollectionUtil.isEmpty(takeStockDTO.getTakeStockSpuList())) {
            throw new LuckException("盘点商品不能为空");
        }
        takeStockService.finishTakeStock(takeStockDTO);
        return ServerResponseEntity.success(Boolean.TRUE);
    }

    @PutMapping("/voided_inventory/{takeStockId}")
    @Operation(summary = "作废盘点", description = "作废盘点")
    public ServerResponseEntity<Boolean> voidedInventory(@PathVariable("takeStockId") Long takeStockId) {
        TakeStockVO takeStockVO = takeStockService.getByTakeStockId(takeStockId);
        if (!Objects.equals(takeStockVO.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException("无权限作废");
        }
        TakeStock takeStock = new TakeStock();
        takeStock.setTakeStockId(takeStockId);
        takeStock.setBillStatus(TakeStockBillStatus.VOIDED.value());
        takeStockService.update(takeStock);
        return ServerResponseEntity.success(true);
    }

    @GetMapping("/export_take_stock")
    @Operation(summary = "实物盘点Excel导出", description = "实物盘点Excel导出")
    public void exportTakeStock(TakeStockDTO takeStockDTO, HttpServletResponse response) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        takeStockService.exportExcel(takeStockDTO, response);
    }
}
