package org.jeecg.modules.inventory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.inventory.convert.ItdmSampleExpireConvert;
import org.jeecg.modules.inventory.dto.ItdmSampleExpireDTO;
import org.jeecg.modules.inventory.entity.ItdmSampleExpire;
import org.jeecg.modules.inventory.entity.ItdmSampleExpireReal;
import org.jeecg.modules.inventory.entity.ItdmSampleInventory;
import org.jeecg.modules.inventory.service.IItdmSampleExpireRealService;
import org.jeecg.modules.inventory.service.IItdmSampleExpireService;
import org.jeecg.modules.inventory.service.IItdmSampleInventoryService;
import org.jeecg.modules.inventory.vo.ItdmSampleExpireListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Description: 样品过期表
 * @Author: jeecg-boot
 * @Date:   2023-06-26
 * @Version: V1.0
 */
@Api(tags="样品过期表")
@RestController
@RequestMapping("/inventory/itdmSampleExpire")
@Slf4j
public class ItdmSampleExpireController extends JeecgController<ItdmSampleExpire, IItdmSampleExpireService> {
	@Autowired
	private IItdmSampleExpireService itdmSampleExpireService;
	@Autowired
	private IItdmSampleExpireRealService realService;
	@Autowired
	private IItdmSampleInventoryService inventoryService;

	/**
	 * 分页列表查询
	 *
	 * @param itdmSampleExpire
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "样品过期表-分页列表查询")
	@ApiOperation(value="样品过期表-分页列表查询", notes="样品过期表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<Page<ItdmSampleExpireListVO>> queryPageList(ItdmSampleExpire itdmSampleExpire,
															   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															   HttpServletRequest req) {
		QueryWrapper<ItdmSampleExpire> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(itdmSampleExpire.getAuditStatus()),"audit_status",itdmSampleExpire.getAuditStatus());
		queryWrapper.like(StringUtils.isNotBlank(itdmSampleExpire.getBarCodes()), "bar_codes", itdmSampleExpire.getBarCodes());
		queryWrapper.orderByDesc("create_time");
		Page<ItdmSampleExpire> page = new Page<ItdmSampleExpire>(pageNo, pageSize);
		Page<ItdmSampleExpire> pageList = itdmSampleExpireService.page(page, queryWrapper);
		List<ItdmSampleExpireListVO> list = pageList.getRecords().stream().map(i->{
			List<String> barCodeList = Pattern.compile("\\s*,\\s*").splitAsStream(i.getBarCodes()).collect(Collectors.toList());;
			return ItdmSampleExpireConvert.INSTANCE.convert1(i,barCodeList);
		}).collect(Collectors.toList());
		Page<ItdmSampleExpireListVO> voPageList = ItdmSampleExpireConvert.INSTANCE.convertPage(pageList,list);
		return Result.OK(voPageList);
	}

	 @ApiOperation(value="分页列表查询待审核", notes="分页列表查询待审核")
	 @GetMapping(value = "/shenheList")
	 public Result<IPage<ItdmSampleExpire>> queryPageShenheList(ItdmSampleExpire itdmSampleExpire,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  HttpServletRequest req) {
//		QueryWrapper<ItdmSampleExpire> queryWrapper = QueryGenerator.initQueryWrapper(itdmSampleExpire, req.getParameterMap());
		QueryWrapper<ItdmSampleExpire> queryWrapper = new QueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(itdmSampleExpire.getBarCodes()),"bar_codes",itdmSampleExpire.getBarCodes());
		queryWrapper.eq("audit_status","0");
		queryWrapper.like(StringUtils.isNotBlank(itdmSampleExpire.getHandlingMethod()), "handling_method", itdmSampleExpire.getHandlingMethod());
		queryWrapper.orderByDesc("create_time");

		Page<ItdmSampleExpire> page = new Page<ItdmSampleExpire>(pageNo, pageSize);
		IPage<ItdmSampleExpire> pageList = itdmSampleExpireService.page(page, queryWrapper);
		return Result.OK(pageList);
	 }

	/**
	 *   添加
	 */
	@AutoLog(value = "样品过期表-添加")
	@ApiOperation(value="样品过期表-添加", notes="样品过期表-添加")
	//@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ItdmSampleExpireDTO dto) {
		List<String> barcodeList = dto.getBarCodes();
		String barcodes = barcodeList.stream().collect(Collectors.joining(","));
		ItdmSampleExpire itdmSampleExpire = ItdmSampleExpireConvert.INSTANCE.convert(dto,barcodes);
		String samples = "";
		for(String barcode:barcodeList){
			ItdmSampleInventory inventory = inventoryService.getByBarCode(barcode);
			if (samples.equals("")) samples = inventory.getSampleName();
			else samples = samples+","+inventory.getSampleName();
		}
		itdmSampleExpire.setSamples(samples);
		itdmSampleExpireService.save(itdmSampleExpire);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 */
	@AutoLog(value = "样品过期表-编辑")
	@ApiOperation(value="样品过期表-编辑", notes="样品过期表-编辑")
	//@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ItdmSampleExpireDTO dto) {
		List<String> barcodeList = dto.getBarCodes();
		String barcodes = barcodeList.stream().collect(Collectors.joining(","));
		ItdmSampleExpire itdmSampleExpire = ItdmSampleExpireConvert.INSTANCE.convert(dto,barcodes);

		if(itdmSampleExpire.getAuditStatus().equals("0")){ //0未审批1审批通过2审批拒绝
			String samples = "";
			for(String barcode:barcodeList){
				ItdmSampleInventory inventory = inventoryService.getByBarCode(barcode);
				if (samples.equals("")) samples = inventory.getSampleName();
				else samples = samples+","+inventory.getSampleName();
			}
			itdmSampleExpire.setSamples(samples);
			itdmSampleExpireService.updateById(itdmSampleExpire);
			return Result.OK("编辑成功!");
		}else return Result.error("已审批,不可修改!");
	}

	 /**
	  *  审核
	  *
	  * @param itdmSampleExpire
	  * @return
	  */
	 @AutoLog(value = "样品过期表-审核")
	 @ApiOperation(value="样品过期表-审核", notes="样品过期表-审核")
	 //@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:edit")
	 @RequestMapping(value = "/shenhe", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> shenhe(@RequestBody ItdmSampleExpire itdmSampleExpire) {
		 if(itdmSampleExpire.getAuditStatus().equals("1")){ //审批通过

			 List<ItdmSampleExpireReal> reals = new ArrayList<>();
			 List<String> barcodeIdList = Arrays.asList(itdmSampleExpire.getBarCodes().split(","));
			 for(String barcode:barcodeIdList){
				 ItdmSampleExpireReal real = new ItdmSampleExpireReal();
				 real.setExpireId(itdmSampleExpire.getId());
				 real.setBarCode(barcode);
				 real.setHandlingMethod(itdmSampleExpire.getHandlingMethod());
				 reals.add(real);
			 }
			 realService.saveBatch(reals);
		 }
		 itdmSampleExpireService.updateById(itdmSampleExpire);
		 return Result.OK("审批成功!");
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "样品过期表-通过id删除")
	@ApiOperation(value="样品过期表-通过id删除", notes="样品过期表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ItdmSampleExpire itdmSampleExpire = itdmSampleExpireService.getById(id);
		if(itdmSampleExpire.getAuditStatus().equals("0")){
			itdmSampleExpireService.removeById(id);
			return Result.OK("删除成功!");
		}else return Result.error("已审批，不可删除！");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "样品过期表-批量删除")
	@ApiOperation(value="样品过期表-批量删除", notes="样品过期表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		List<String> idList = new ArrayList<>();
		for (String id :list){
			ItdmSampleExpire itdmSampleExpire = itdmSampleExpireService.getById(id);
			if(itdmSampleExpire.getAuditStatus().equals("0")) idList.add(id);
		}
		this.itdmSampleExpireService.removeByIds(idList);
		return Result.OK("未审批部分批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "样品过期表-通过id查询")
	@ApiOperation(value="样品过期表-通过id查询", notes="样品过期表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ItdmSampleExpire> queryById(@RequestParam(name="id",required=true) String id) {
		ItdmSampleExpire itdmSampleExpire = itdmSampleExpireService.getById(id);
		if(itdmSampleExpire==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(itdmSampleExpire);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param itdmSampleExpire
    */
    //@RequiresPermissions("org.jeecg.modules:itdm_sample_expire:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ItdmSampleExpire itdmSampleExpire) {
        return super.exportXls(request, itdmSampleExpire, ItdmSampleExpire.class, "样品过期表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("itdm_sample_expire:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ItdmSampleExpire.class);
    }

}
