package com.lframework.xingyun.sc.controller.serial;

import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.web.annotations.security.HasPermission;
import com.lframework.starter.web.components.excel.ExcelMultipartWriterSheetBuilder;
import com.lframework.starter.web.controller.DefaultBaseController;
import com.lframework.starter.web.resp.InvokeResult;
import com.lframework.starter.web.resp.InvokeResultBuilder;
import com.lframework.starter.web.resp.PageResult;
import com.lframework.starter.web.utils.ExcelUtil;
import com.lframework.starter.web.utils.IdUtil;
import com.lframework.starter.web.utils.PageResultUtil;
import com.lframework.xingyun.sc.bo.purchase.returned.QueryPurchaseReturnBo;
import com.lframework.xingyun.sc.bo.stock.product.QueryProductStockBo;
import com.lframework.xingyun.sc.entity.Page;
import com.lframework.xingyun.sc.entity.ProductStock;
import com.lframework.xingyun.sc.entity.PurchaseReturn;
import com.lframework.xingyun.sc.entity.SerialNumber;
import com.lframework.xingyun.sc.excel.stock.ProductStockExportModel;
import com.lframework.xingyun.sc.mappers.SerialNumberMapper;
import com.lframework.xingyun.sc.service.stock.ProductStockService;
import com.lframework.xingyun.sc.vo.purchase.returned.QueryPurchaseReturnVo;
import com.lframework.xingyun.sc.vo.serial.SerialNumberByInBillNoVo;
import com.lframework.xingyun.sc.vo.serial.SerialNumberLikeNameVo;
import com.lframework.xingyun.sc.vo.serial.SerialNumberListVo;
import com.lframework.xingyun.sc.vo.serial.SerialNumberVo;
import com.lframework.xingyun.sc.vo.stock.QueryProductStockVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品库存
 *
 * @author zmj
 */
@Api(tags = "商品序列号")
@Validated
@RestController
@RequestMapping("/stock/serial")
public class SerialNumerController extends DefaultBaseController {

    @Autowired
    private SerialNumberMapper serialNumberMapper;


  /**
   * 退单列表
   */
  @ApiOperation("查询商品序列号")
  @PostMapping("/getSerialNumberList")
  public InvokeResult<Page<List<SerialNumber>>> getSerialNumberList(@RequestBody SerialNumberListVo vo) {

    Integer count=serialNumberMapper.getSerialNumberCount(vo.getSerialId(),vo.getSerialNumber(),vo.getProductId());
    Integer startNum=(vo.getPageNum()-1)*vo.getPageSize();

    List<SerialNumber> list=serialNumberMapper.getSerialNumberList(vo.getSerialId(),vo.getSerialNumber(),vo.getProductId(),startNum,vo.getPageSize());

    return InvokeResultBuilder.success(new Page(vo.getPageNum(),count,vo.getPageSize(),list));
  }

    @ApiOperation("出库-移除序列号")
    @PostMapping("/updateOutBillNoClean")
    public InvokeResult<Void> updateOutBillNoClean(@RequestBody SerialNumberLikeNameVo vo) {

       serialNumberMapper.updateOutBillNoClean(vo.getSerialId());

       return InvokeResultBuilder.success();
    }


  @ApiOperation("入库-移除序列号")
  @PostMapping("/updateInBillNoClean")
  public InvokeResult<Void> updateInBillNoClean(@RequestBody SerialNumberLikeNameVo vo) {

    serialNumberMapper.updateInBillNoClean(vo.getSerialId());

    return InvokeResultBuilder.success();
  }

  @ApiOperation("添加列号")
  @PostMapping("/insertInBillNo")
  public InvokeResult<Void> insertInBillNo(@RequestBody SerialNumberVo vo) {

    if(vo.getSerialNumber()!=null && vo.getSerialNumber()!=""){
      String[] valueArray =  vo.getSerialNumber().split(",");

      if(vo.getBizType()==0){
        for (String value : valueArray) {
          SerialNumber serialNumber = new SerialNumber();
          serialNumber.setId(IdUtil.getId());
          serialNumber.setSerialNumber(value.trim()); // 去除空格
          serialNumber.setCenterId(vo.getScId());
          serialNumber.setCreateTime(LocalDateTime.now());
          serialNumber.setAdjustId(vo.getId());
          serialNumber.setProductId(vo.getProductId());
          serialNumber.setInBillNo(vo.getId());
          serialNumberMapper.insert(serialNumber); // 插入数据库
        }
      }else if(vo.getBizType()==2){
        for (String value : valueArray) {
          SerialNumber serialNumber = new SerialNumber();
          serialNumber.setId(IdUtil.getId());
          serialNumber.setSerialNumber(value.trim()); // 去除空格
          serialNumber.setCenterId(vo.getScId());
          serialNumber.setCreateTime(LocalDateTime.now());
          serialNumber.setAdjustId(vo.getId());
          serialNumber.setProductId(vo.getProductId());
          serialNumber.setOutBillNo(vo.getId());
          serialNumberMapper.insert(serialNumber); // 插入数据库
        }
      }
    }


    return InvokeResultBuilder.success();
  }

//  @ApiOperation("出库-添加序列号")
//  @PostMapping("/insertOutBillNo")
//  public InvokeResult<Void> insertOutBillNo(@RequestBody SerialNumberVo vo) {
//
//    serialNumberMapper.updateInBillNoClean(vo.getSerialId());
//
//    return InvokeResultBuilder.success();
//  }

}
