package com.shkj.wms.controller.base;

import com.shkj.common.annotation.Log;
import com.shkj.common.annotation.RepeatSubmit;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.exception.ServiceException;
import com.shkj.common.utils.StringUtils;
import com.shkj.common.utils.poi.ExcelUtil;
import com.shkj.wms.bo.BaseMoldAddBo;
import com.shkj.wms.bo.BaseMoldBarcodeQueryBo;
import com.shkj.wms.bo.BaseMoldBarcodeQueryPageBo;
import com.shkj.wms.bo.BaseMoldQueryBo;
import com.shkj.wms.common.IdEntity;
import com.shkj.wms.common.SwitchStatusEntity;
import com.shkj.wms.config.ValidList;
import com.shkj.wms.constants.RedisLockConstants;
import com.shkj.wms.domain.SysStockDetail;
import com.shkj.wms.service.IBaseMoldService;
import com.shkj.wms.utils.BarcodeUtils;
import com.shkj.wms.vo.BaseMoldVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.shkj.common.core.redis.RedisCache;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 磨具管理
 *
 * @author lch
 * @menu 磨具管理
 * @date 2023-06-01
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wms/mold")
public class BaseMoldController extends BaseController {

    private final IBaseMoldService iBaseMoldService;

    @Autowired
    RedisCache redisCache;

    /**
     * 查询模具列表
     */
    @GetMapping("/list")
    public TableDataInfo<BaseMoldVo> list(BaseMoldQueryBo bo) {
        startPage();
        List<BaseMoldVo> list = iBaseMoldService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出磨具列表
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, BaseMoldQueryBo bo) {
        List<BaseMoldVo> list = iBaseMoldService.queryList(bo);
        ExcelUtil<BaseMoldVo> util = new ExcelUtil<BaseMoldVo>(BaseMoldVo.class);
        util.exportExcel(response, list, "磨具数据");
    }

    /**
     * 获取磨具详细信息
     */
    @GetMapping("/{id}")
    public Result<BaseMoldVo> getInfo(@PathVariable("id") Long id) {
        return iBaseMoldService.queryById(id);
    }

    /**
     * 新增磨具
     */
    @Log(title = "新增磨具", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@Valid @RequestBody BaseMoldAddBo bo) {
        return iBaseMoldService.insertByAddBo(bo);
    }

    /**
     * 修改磨具
     */
    @Log(title = "修改磨具", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result<Boolean> edit(@Valid @RequestBody BaseMoldAddBo bo) {
        if (bo.getId() == null) {
            return Result.err().msg("没有主键信息");
        }
        return iBaseMoldService.updateByEditBo(bo);
    }

    /**
     * 删除磨具
     */
    @Log(title = "删除磨具", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public Result<Boolean> remove(@Valid @RequestBody ValidList<Long> ids) {
        return iBaseMoldService.deleteWithValidById(ids, true);
    }


    /**
     * 切换状态
     */
    @Log(title = "切换状态", businessType = BusinessType.UPDATE)
    @PostMapping("/switchStatus")
    public Result<Boolean> switchStatus(@Valid @RequestBody SwitchStatusEntity switchStatusEntity) {
        return iBaseMoldService.switchStatus(switchStatusEntity.getId(), switchStatusEntity.getIsuse());
    }


    /**
     * 打印条码返回流
     */
    @Log(title = "打印条码", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/printBarcode")
    public void printBarcode(HttpServletResponse response, @Valid @RequestBody IdEntity idEntity) throws IOException {
        Result result = iBaseMoldService.printBarcode(idEntity.getId());
        if (!Result.isOk(result)) {
            throw new ServiceException(result.getMsg());
        }
        BarcodeUtils.generateBarCode128(result.getData().toString(), 10.0, 0.3, true, false, response.getOutputStream());
    }

    /**
     * 打印条码返回字符串
     */
    @Log(title = "打印条码", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/printBarcodeReturnCode")
    public Result printBarcode(@Valid @RequestBody IdEntity idEntity) {
        return iBaseMoldService.printBarcode(idEntity.getId());
    }

    /**
     * 打印条码返回集合字符串
     */
    @Log(title = "打印条码集合", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/printBarcodeListReturnCode")
    public Result printBarcode(@Valid @RequestBody List<IdEntity> idEntities) {
        List<String> list = new ArrayList<>();
        for (IdEntity entity : idEntities) {
            Result<String> barcode = iBaseMoldService.printBarcode(entity.getId());
            if (!Result.isOk(barcode)) {
                return Result.err().msg(barcode.getMsg());

            }
            list.add(barcode.getData());
        }
        return Result.ok().data(list);
    }

    /**
     * 根据模具条码查询模具信息
     */

    @GetMapping("/queryMoldByBarCode")
    public Result<List<BaseMoldVo>> queryMoldByBarCode(BaseMoldBarcodeQueryBo bo) {

        return iBaseMoldService.queryMoldByBarCode(bo);
    }

    /**
     * 根据托盘代码获取托盘中的模具信息
     * @param bo
     * @return
     */
    @GetMapping("/queryMoldByContainerCode")
    public Result<List<BaseMoldVo>> queryMoldByContainerCode(BaseMoldBarcodeQueryBo bo) {

        return iBaseMoldService.queryMoldByContainerCode(bo);
    }

    /**
     * 根据模具条码查询模具信息（一定有库存）
     */

    @GetMapping("/queryStockMoldByBarCode")
    public Result<List<BaseMoldVo>> queryStockMoldByBarCode(BaseMoldBarcodeQueryBo bo) {
        return iBaseMoldService.queryStockMoldByBarCode(bo);
    }

   @GetMapping("/queryStockMoldByBarCodePc")
    public TableDataInfo<BaseMoldVo> queryStockMoldByBarCodePc(BaseMoldBarcodeQueryPageBo bo) {
        startPage();
        return getDataTable( iBaseMoldService.queryStockMoldByBarCodePc(bo));
    }

   /* @GetMapping("/queryStockMoldByBarCodePc")
    public Result<List<BaseMoldVo>> queryStockMoldByBarCodePc(BaseMoldBarcodeQueryBo bo) {
//        if (StringUtils.isBlank(bo.getMoldBarcode())) {
//            return Result.err().msg("模具编码不得为空");
//        }
        return iBaseMoldService.queryStockMoldByBarCode(bo);
    }*/

    /**
     *  新增入库计划时，过滤掉已经在库的模具信息
     */
    @GetMapping("/getBaseMoldNotinStockDetail")
    public TableDataInfo<BaseMoldVo> getBaseMoldNotinStockDetail(BaseMoldQueryBo bo) {
        startPage();
        List<BaseMoldVo> list = iBaseMoldService.getBaseMoldNotinStockDetail(bo);
        return getDataTable(list);
    }
}
