package org.springblade.app;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.pl.equipment.entity.EquipmentEntity;
import org.springblade.modules.pl.equipment.service.IEquipmentService;
import org.springblade.modules.pl.factory.entity.FactoryEntity;
import org.springblade.modules.pl.factory.service.IFactoryService;
import org.springblade.modules.pl.goods.entity.GoodsMaterialEntity;
import org.springblade.modules.pl.goods.service.IGoodsMaterialService;
import org.springblade.modules.pl.goods.wrapper.GoodsMaterialWrapper;
import org.springblade.modules.pl.inspectionRecords.entity.InspectionDetailsEntity;
import org.springblade.modules.pl.inspectionRecords.entity.InspectionRecordsEntity;
import org.springblade.modules.pl.inspectionRecords.service.IInspectionDetailsService;
import org.springblade.modules.pl.inspectionRecords.service.IInspectionRecordsService;
import org.springblade.modules.pl.inspectionRecords.vo.InspectionDetailsVO;
import org.springblade.modules.pl.inspectionRecords.wrapper.InspectionDetailsWrapper;
import org.springblade.modules.pl.inspectionRecords.wrapper.InspectionRecordsWrapper;
import org.springblade.modules.pl.product.entity.ProductEntity;
import org.springblade.modules.pl.product.entity.ProductImageRecordEntity;
import org.springblade.modules.pl.product.service.IProductImageRecordService;
import org.springblade.modules.pl.product.service.IProductService;
import org.springblade.modules.pl.product.vo.ProductImageRecordVO;
import org.springblade.modules.pl.product.vo.ProductVO;
import org.springblade.modules.pl.product.wrapper.ProductImageRecordWrapper;
import org.springblade.modules.pl.product.wrapper.ProductWrapper;
import org.springblade.modules.pl.productModel.entity.ProductModelEntity;
import org.springblade.modules.pl.productModel.service.IProductModelService;
import org.springblade.modules.pl.productRework.entity.ProductReworkEntity;
import org.springblade.modules.pl.productRework.service.IProductReworkService;
import org.springblade.modules.pl.productRework.vo.ProductReworkVO;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("app/api")
@Api(value = "app接口", tags = "app相关外放接口")
@Slf4j
public class AppApiController {
	private final IProductImageRecordService productImageRecordService;
	private final IEquipmentService equipmentService;
	private final IFactoryService factoryService;
	private final IInspectionRecordsService inspectionRecordsService;
	private final IInspectionDetailsService inspectionDetailsService;
	private final IGoodsMaterialService goodsMaterialService;
	private final IProductService productService;
	private final IProductReworkService productReworkService;
	private final IProductModelService productModelService;

	/**
	 * 获取设备信息 分页
	 */
	@GetMapping("/getEquipmentInfo")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "产品拍照", notes = "传入product")
	public R<?> getEquipmentInfo(@ApiIgnore @RequestParam Map<String, Object> param, Query query) {
		if(param.get("eqSn_equal")==null){
			return R.fail("设备序列号不能为空");
		}
		query.setDescs("create_time");
		QueryWrapper<EquipmentEntity> wrapper = Condition.getQueryWrapper(param,EquipmentEntity.class);
		EquipmentEntity equipment = equipmentService.getOne(wrapper,false);
		if(equipment==null){
			return R.fail("设备不存在，请联系管理员");
		}
		FactoryEntity factory = factoryService.getById(equipment.getFactoryId());
		if(factory==null){
			return R.fail("设备信息异常，请联系管理员");
		}
		Map<String,Object> map = new HashMap<>();
		map.put("equipment",equipment);
		map.put("factory",factory);
		return R.data(map);
	}

	/**
	 * 产品拍照 分页
	 */
	@GetMapping("/productImgList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "产品拍照", notes = "传入product")
	public R<?> productImgList(@ApiIgnore @RequestParam Map<String, Object> param, Query query) {
		if(param.get("eqSn_equal")==null){
			return R.fail("设备序列号不能为空");
		}
		query.setDescs("create_time");
		QueryWrapper<ProductImageRecordEntity> wrapper = Condition.getQueryWrapper(param,ProductImageRecordEntity.class);
		IPage<ProductImageRecordEntity> pages = productImageRecordService.page(Condition.getPage(query),wrapper);
		return R.data(ProductImageRecordWrapper.build().pageVO(pages));
	}


	/**
	 * 商品点检记录 分页
	 */
	@GetMapping("/inspectionRecordsList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "产品拍照", notes = "传入product")
	public R<?> inspectionRecordsList(@ApiIgnore @RequestParam Map<String, Object> param, Query query) {
		if (param.get("eqSn_equal") == null || "".equals(param.get("eqSn_equal"))) {
			return R.fail("设备序列号不能为空");
		}
		query.setDescs("create_time");
		QueryWrapper<InspectionRecordsEntity> wrapper = Condition.getQueryWrapper(param,InspectionRecordsEntity.class);
		IPage<InspectionRecordsEntity> pages = inspectionRecordsService.page(Condition.getPage(query),wrapper);
		return R.data(InspectionRecordsWrapper.build().pageVO(pages));
	}

	/**
	 * 商品点检记录详情
	 */
	@GetMapping("/getInspectionDetails")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "产品拍照", notes = "传入product")
	public R<?> getInspectionDetails(@ApiIgnore @RequestParam Map<String, Object> param) {
		if (param.get("inspectionRecordsId_equal") == null || "".equals(param.get("inspectionRecordsId_equal"))) {
			return R.fail("点检记录不能为空");
		}
		QueryWrapper<InspectionDetailsEntity> wrapper = Condition.getQueryWrapper(param,InspectionDetailsEntity.class);
		List<InspectionDetailsEntity> list = inspectionDetailsService.list(wrapper);
		List<InspectionDetailsVO> details = new ArrayList<>();
		for(InspectionDetailsEntity detail : list){
			InspectionDetailsVO inspectionDetailsVO = InspectionDetailsWrapper.build().entityVO(detail);
			GoodsMaterialEntity goodsMaterial = goodsMaterialService.getById(detail.getMaterialId());
			if(goodsMaterial!=null){
				inspectionDetailsVO.setCorrectMaterialCode(goodsMaterial.getCode());
			}
			details.add(inspectionDetailsVO);
		}
		return R.data(details);
	}

	/**
	 * 修改点检详情
	 */
	@PostMapping("/editInspectionDetails")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "产品拍照", notes = "传入product")
	public R<?> editInspectionDetails (@RequestBody List<InspectionDetailsEntity> list){
		if(list==null || list.size()<0){
			return R.fail("请输入物料信息");
		}
		for(InspectionDetailsEntity detail : list){
			GoodsMaterialEntity goodsMaterial = goodsMaterialService.getById(detail.getMaterialId());
			if(goodsMaterial==null){
				return R.fail("物料信息异常，请联系管理员");
			}
			if(!goodsMaterial.getCode().equals(detail.getMaterialCode())){
				return R.fail(goodsMaterial.getName()+"物料编码错误，请重新输入");
			}
			detail.setStatus(1);
		}
		inspectionDetailsService.updateBatchById(list);
		InspectionRecordsEntity inspectionRecords = new InspectionRecordsEntity();
		inspectionRecords.setId(list.get(0).getInspectionRecordsId());
		inspectionRecords.setStatus(3);
		inspectionRecordsService.updateById(inspectionRecords);
		return R.data(list);
	}

	/**
	 * 产品NG或OK操作
	 */
	@GetMapping("/productOkOrNg")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "产品NG或OK操作", notes = "传入productBatch")
	public R productOkOrNg(@Valid @RequestParam String productId,@RequestParam String aiImg,@RequestParam String isNg) throws InterruptedException {
		if(productId==null || isNg==null || aiImg==null){
			return R.fail("请传入产品信息");
		}
		ProductEntity productEntity = productService.getById(productId);
		if(productEntity==null){
			return R.fail("NG的产品不存在");
		}
		if(productEntity.getStatus()!=2 && (productEntity.getAiStatus()==0 || productEntity.getAiStatus()==1)){
			return R.fail("当前产品状态无法操作");
		}
		if("0".equals(isNg)){
			productEntity.setStatus(2);
			productEntity.setAiStatus(3);
			productService.updateById(productEntity);
		}else{
			productEntity.setAiStatus(1);
			productEntity.setStatus(1);
			productService.updateById(productEntity);
			ProductReworkEntity productReworkEntity = new ProductReworkEntity();
			productReworkEntity.setFactoryId(productEntity.getFactoryId());
			productReworkEntity.setProductId(productEntity.getId());
			productReworkEntity.setQrCode(productEntity.getQrCode());
			productReworkEntity.setProductModelId(productEntity.getProductModelId());
			productReworkEntity.setProductName(productEntity.getProductName());
			productReworkEntity.setProductModel(productEntity.getProductModel());
			productReworkEntity.setRepairsImgs(aiImg);
			productReworkEntity.setProblemDesc("AI检测NG");
			productReworkEntity.setStatus(1);
			productReworkEntity.setCreatePersonName("AI检测");
			productReworkService.save(productReworkEntity);

		}
		ProductModelEntity productModel = productModelService.getById(productEntity.getProductModelId());

		if(productEntity!=null && productModel!=null && productModel.getIsPushTripartite()==1){
			if(productEntity.getAiStatus()==-1 || productEntity.getAiStatus()==2){
				productService.pushTripartite(productEntity.getQrCode(),aiImg,productModel,"OK");
			}
		}

		return R.success("操作成功");
	}
}
