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

import com.alibaba.excel.EasyExcel;
import com.tmerclub.cloud.common.constant.Constant;
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.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.dto.TakeStockSpuDTO;
import com.tmerclub.cloud.product.listener.TakeStockSpuExcelListener;
import com.tmerclub.cloud.product.model.TakeStock;
import com.tmerclub.cloud.product.model.TakeStockSpu;
import com.tmerclub.cloud.product.service.TakeStockService;
import com.tmerclub.cloud.product.service.TakeStockSpuService;
import com.tmerclub.cloud.product.vo.TakeStockSpuExcelVO;
import com.tmerclub.cloud.product.vo.TakeStockSpuImportVO;
import com.tmerclub.cloud.product.vo.TakeStockSpuVO;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 盘点商品列表
 *
 * @author lhd
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopTakeStockSpuController")
@RequestMapping("/m/take_stock_spu")
@Tag(name = "盘点商品列表")
public class TakeStockSpuController {

    @Autowired
    private TakeStockService takeStockService;

    @Autowired
    private TakeStockSpuService takeStockSpuService;


    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('shop:takeStockSpu:page')")
    @Operation(summary = "获取盘点商品列表列表", description = "分页获取盘点商品列表列表")
    public ServerResponseEntity<PageVO<TakeStockSpu>> page(@Valid PageDTO pageDTO) {
        PageVO<TakeStockSpu> takeStockSpuPage = takeStockSpuService.page(pageDTO);
        return ServerResponseEntity.success(takeStockSpuPage);
    }

    @GetMapping("/list_by_take_stock_id")
    @Operation(summary = "通过takeStockId获取盘点商品列表", description = "通过takeStockId获取盘点商品列表")
    public ServerResponseEntity<List<TakeStockSpuVO>> listByTakeStockId(Long takeStockId) {
        List<TakeStockSpuVO> takeStockSpuVOList = takeStockSpuService.listByTakeStockId(takeStockId);
        return ServerResponseEntity.success(takeStockSpuVOList);
    }

    @GetMapping
    @Operation(summary = "获取盘点商品信息", description = "根据takeStockSpuId获取盘点商品信息")
    public ServerResponseEntity<TakeStockSpu> getByTakeStockSpuId(@RequestParam Long takeStockSpuId) {
        return ServerResponseEntity.success(takeStockSpuService.getByTakeStockSpuId(takeStockSpuId));
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('shop:takeStockSpu:save')")
    @Operation(summary = "保存盘点商品列表", description = "保存盘点商品列表")
    public ServerResponseEntity<Void> save(@Valid @RequestBody TakeStockSpuDTO takeStockSpuDTO) {
        TakeStockSpu takeStockSpu = BeanUtil.map(takeStockSpuDTO, TakeStockSpu.class);
        takeStockSpu.setTakeStockSpuId(null);
        takeStockSpuService.save(takeStockSpu);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新盘点商品列表", description = "更新盘点商品列表")
    public ServerResponseEntity<Void> update(@Valid @RequestBody TakeStockSpuDTO takeStockSpuDTO) {
        TakeStockSpu takeStockSpu = BeanUtil.map(takeStockSpuDTO, TakeStockSpu.class);
        takeStockSpuService.update(takeStockSpu);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除盘点商品列表", description = "根据盘点商品列表id删除盘点商品列表")
    public ServerResponseEntity<Void> delete(@RequestParam Long takeStockSpuId) {
        takeStockSpuService.deleteById(takeStockSpuId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/download_model")
    @Operation(summary = "实物盘点商品Excel模板下载", description = "实物盘点商品Excel模板下载")
    public void downLoadModel(HttpServletResponse response) {
        takeStockSpuService.downloadModel(response);
    }

    @Operation(summary = "导入盘点商品", description = "导入盘点商品")
    @PostMapping("/importExcel")
    public ServerResponseEntity<Object> importExcel(@RequestParam("excelFile") MultipartFile excelFile,
                                                    @RequestParam("takeStockId") Long takeStockId) {
        if (Objects.isNull(excelFile)) {
            throw new LuckException("yami.network.busy");
        }
        if (Objects.isNull(takeStockId)) {
            throw new LuckException("yami.supplier.not.null");
        }
        TakeStockSpuImportVO takeStockSpuImportVO = new TakeStockSpuImportVO();
        try {
            // 错误信息集合
            List<String> errorList = new ArrayList<>(Constant.INITIAL_CAPACITY);
            EasyExcel.read(excelFile.getInputStream(), TakeStockSpuExcelVO.class, new TakeStockSpuExcelListener(takeStockSpuService, errorList, takeStockId, takeStockSpuImportVO)).sheet().doRead();
        } catch (IOException e) {
            throw new LuckException(e.getMessage());
        }
        return ServerResponseEntity.success(takeStockSpuImportVO);
    }

    @GetMapping("/export_take_stock_spu")
    @Operation(summary = "实物盘点商品Excel导出", description = "实物盘点Excel导出")
    public void exportTakeStock(Long takeStockId, HttpServletResponse response) {
        if (takeStockId == null) {
            throw new LuckException("盘点id不能为空");
        }
        TakeStockVO takeStock = takeStockService.getByTakeStockId(takeStockId);
        if (takeStock == null) {
            throw new LuckException("实物盘点不存在");
        }
        takeStockSpuService.exportExcel(takeStockId, response);
    }

}
