package org.springblade.applet;

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.core.tool.utils.Func;
import org.springblade.modules.pl.client.entity.ClientEntity;
import org.springblade.modules.pl.client.service.IClientService;
import org.springblade.modules.pl.product.entity.ProductEntity;
import org.springblade.modules.pl.product.service.IProductService;
import org.springblade.modules.pl.product.vo.ProductVO;
import org.springblade.modules.pl.product.wrapper.ProductWrapper;
import org.springblade.modules.pl.productBatch.service.IProductBatchService;
import org.springblade.modules.pl.productBatch.vo.ProductBatchVO;
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.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

@RestController
@AllArgsConstructor
@RequestMapping("wx/product/api")
@Api(value = "产品", tags = "产品相关外放接口")
@Slf4j
public class ProductApiController {

	private final IProductBatchService productBatchService;

	private final IProductService productService;

	private final IProductModelService productModelService;

	private final IClientService clientService;

	private final IProductReworkService productReworkService;

	/**
	 * 产品表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入product")
	public R<IPage<ProductVO>> list(@ApiIgnore @RequestParam Map<String, Object> product, Query query) {
		query.setDescs("create_time");
		BladeUser user = AuthUtil.getUser();
		product.put("factoryId",user.getFactoryId());
		QueryWrapper<ProductEntity> wrapper = Condition.getQueryWrapper(product,ProductEntity.class);
		if("1".equals(product.get("isNg_ignore"))){
			wrapper.eq("status", 2);
			wrapper.in("ai_status", Func.toLongList("0,1"));
		}
		IPage<ProductEntity> pages = productService.page(Condition.getPage(query),wrapper);
		return R.data(ProductWrapper.build().pageVO(pages));
	}

	/**
	 * 产品表 统计
	 */
	@GetMapping("/countProduct")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入product")
	public R<Object> countProduct(@ApiIgnore @RequestParam Map<String, Object> product, Query query) {
		BladeUser user = AuthUtil.getUser();
		long countAll = productService.count(Wrappers.<ProductEntity>lambdaQuery().eq(ProductEntity::getFactoryId,user.getFactoryId()).eq(ProductEntity::getStatus,"3"));
		product.put("factoryId",user.getFactoryId());
		QueryWrapper<ProductEntity> wrapper = Condition.getQueryWrapper(product,ProductEntity.class);
		long count = productService.count(wrapper);
		Map<String,Object> result = new HashMap<>();
		result.put("countAll",countAll);
		result.put("count",count);
		return R.data(result);
	}


	/**
	 * 产品表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入product")
	public R<ProductVO> detail(ProductEntity product) {
		BladeUser user = AuthUtil.getUser();
		product.setFactoryId(user.getFactoryId());
		ProductEntity detail = productService.getOne(Condition.getQueryWrapper(product));
		if(detail==null){
			return R.fail("未查询到产品信息");
		}
		return R.data(ProductWrapper.build().entityVO(detail));
	}


	/**
	 * 产品入库批次表 新增或修改
	 */
	@PostMapping("/batchSubmit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入productBatch")
	public R submit(@Valid @RequestBody ProductBatchVO productBatch) {
		return productBatchService.submit(productBatch);
	}



	/**
	 * 客户表 下拉框
	 */
	@GetMapping("/clientSelect")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "下拉框", notes = "传入factory")
	public R<List<ClientEntity>> select(ClientEntity factory, Query query) {
		BladeUser user = AuthUtil.getUser();
		if(user.getUserCategory()==2){
			factory.setFactoryId(user.getFactoryId());
		}
		List<ClientEntity> list = clientService.list(Condition.getQueryWrapper(factory));
		return R.data(list);
	}


	/**
	 * 产品型号表 下拉框
	 */
	@GetMapping("/modelSelect")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "下拉框", notes = "传入factory")
	public R<List<ProductModelEntity>> select(ProductModelEntity productModel) {
		BladeUser user = AuthUtil.getUser();
		productModel.setFactoryId(user.getFactoryId());
		List<ProductModelEntity> list = productModelService.list(Condition.getQueryWrapper(productModel));
		return R.data(list);
	}

	/**
	 * 产品NG或OK操作
	 */
	@GetMapping("/productNgOperate")
	@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("当前产品状态无法操作");
		}
		BladeUser user = AuthUtil.getUser();
		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(user.getNickName());
			productReworkService.save(productReworkEntity);

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