package com.jeequan.jeepay.mgr.ctrl.product;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.ProductInfo;
import com.jeequan.jeepay.db.entity.ProductType;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.ProductInfoService;
import com.jeequan.jeepay.service.impl.ProductTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 产品分类
 *
 * @author huay
 * @date 2023-11-09 14:50
 */
@RestController
@RequestMapping("/api/productType")
public class ProductTypeController extends CommonCtrl {

	@Autowired private ProductTypeService productTypeService;
	@Autowired private ProductInfoService productInfoService;

	/**
	 * 分类列表
	 * **/
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@MethodLog(remark = "分类列表")
	public ApiRes getList() throws BizException {
		//获取查询条件
		ProductType type = getObject(ProductType.class);
		LambdaQueryWrapper<ProductType> wrapper = ProductType.gw();
		selectParams(type, wrapper);
		wrapper.orderByDesc(ProductType::getCreatedAt);
		Page<ProductType> pages = productTypeService.page(getIPage(true), wrapper);
		return ApiRes.page(pages);
	}

	@MethodLog(remark = "新增分类")
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public ApiRes add() {
		ProductType type = getObject(ProductType.class);
		// 当前登录用户信息
		SysUser sysUser = getCurrentUser().getSysUser();
		type.setCreatedBy(String.valueOf(sysUser.getSysUserId()));
		productTypeService.save(type);

		return ApiRes.ok();
	}

	@MethodLog(remark = "删除分类")
	@RequestMapping(value="/{typeId}", method = RequestMethod.DELETE)
	public ApiRes delete(@PathVariable("typeId") String typeId) {
		//判断该节点下是否有子节点 有则不让删除
		LambdaQueryWrapper<ProductInfo> wrapper=new LambdaQueryWrapper<>();
		wrapper.eq(ProductInfo::getProductType,typeId);
		wrapper.eq(ProductInfo::getProductState,1);
		List<ProductInfo> productInfoslist = productInfoService.list(wrapper);
		if (!productInfoslist.isEmpty()){
			return ApiRes.customFail("该分类下相关内容未清空！");
		}
		productTypeService.deleteProductType(typeId);
		return ApiRes.ok();
	}


	@RequestMapping(value = "/getDetailById", method = RequestMethod.GET)
	@MethodLog(remark = "回显产品详情")
	public ApiRes getDetailById() throws BizException {
		//获取查询条件
		ProductType productType = getObject(ProductType.class);

		ProductType type  = productTypeService.getById(productType.getTypeId());
		return ApiRes.ok(type);
	}
	@MethodLog(remark = "更新分类信息")
	@RequestMapping(value="/{typeId}", method = RequestMethod.PUT)
	public ApiRes update(@PathVariable("typeId") String typeId) {
		//获取查询条件
		ProductType type = getObject(ProductType.class);
		type.setTypeId(typeId);//设置主键
		type.setUpdatedAt(new Date());
		productTypeService.updateById(type);
		return ApiRes.ok();
	}

	public void selectParams(ProductType type, LambdaQueryWrapper<ProductType> wrapper) {
		wrapper.like(StringUtils.isNotEmpty(type.getTypeName()), ProductType::getTypeName, type.getTypeName());
	}

}
