package com.iotechn.unimall.admin.api.cloud.impl;

import java.util.List;

import com.iotechn.unimall.admin.api.cloud.IProductManagementService;
import com.iotechn.unimall.data.domain.ProductManagement;
import com.iotechn.unimall.data.mapper.ProductManagementMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品管理信息Service业务层处理
 * 
 * @author jlb
 * @date 2023-05-26
 */
@Service
public class ProductManagementServiceImpl implements IProductManagementService {
	@Autowired
	private ProductManagementMapper productManagementMapper;
	
	@Override
	public Boolean add(ProductManagement productManagement) throws ServiceException {
		Date now = new Date();
		productManagement.setGmtCreate(now);
		productManagement.setGmtUpdate(now);
		productManagement.setStatus("未上架");
		return productManagementMapper.insert(productManagement)>0;
	}

	@Override
	public Page<ProductManagement> list(String productTitle, String productIntroduction, String coverImage, String classification, String productTemplate,String searchKeywords,String language, String status, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ProductManagement> wrapper = new EntityWrapper<ProductManagement>();
														if (!StringUtils.isEmpty(productTitle)) {
					wrapper.like("product_title", productTitle);
				}
												if (!StringUtils.isEmpty(productIntroduction)) {
					wrapper.eq("product_introduction", productIntroduction);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(classification)) {
					wrapper.eq("classification", classification);
				}
												if (!StringUtils.isEmpty(productTemplate)) {
					wrapper.eq("product_template", productTemplate);
				}
												if (!StringUtils.isEmpty(searchKeywords)) {
					wrapper.like("search_keywords", searchKeywords);
				}
				if (!StringUtils.isEmpty(language)) {
					wrapper.eq("language", language);
				}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
							wrapper.eq("delete_flag", 0).orderBy("gmt_update",false);
		List<ProductManagement> list = productManagementMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = productManagementMapper.selectCount(wrapper);
		return new Page<ProductManagement>(list, page, limit, count);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean delete(String id) {
		String[] ids = String.valueOf(id).split(",");
		for (String tt:ids) {
			ProductManagement tmp =  productManagementMapper.selectById(Long.parseLong(tt));
			if(tmp != null){
				tmp.setDeleteFlag(1l);
				productManagementMapper.updateById(tmp);
			}
		}
		return true;
	}

	@Override
	public Boolean update(ProductManagement productManagement) throws ServiceException {
		Date now = new Date();
		productManagement.setGmtUpdate(now);
		return productManagementMapper.updateById(productManagement)>0;
	}

	@Override
	public ProductManagement get(Long id) throws ServiceException {
		return productManagementMapper.selectById(id);
	}
	
	@Override
	public String export(String productTitle,String productIntroduction,String coverImage,String classification,String productTemplate,String status,Date gmtCreate,Date gmtUpdate,Long deleteFlag, Integer page, Integer limit)throws ServiceException {
		Wrapper<ProductManagement> wrapper = new EntityWrapper<ProductManagement>();
														if (!StringUtils.isEmpty(productTitle)) {
					wrapper.eq("product_title", productTitle);
				}
												if (!StringUtils.isEmpty(productIntroduction)) {
					wrapper.eq("product_introduction", productIntroduction);
				}
												if (!StringUtils.isEmpty(coverImage)) {
					wrapper.eq("cover_image", coverImage);
				}
												if (!StringUtils.isEmpty(classification)) {
					wrapper.eq("classification", classification);
				}
												if (!StringUtils.isEmpty(productTemplate)) {
					wrapper.eq("product_template", productTemplate);
				}
												if (!StringUtils.isEmpty(status)) {
					wrapper.eq("status", status);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
							List<ProductManagement> list = productManagementMapper.selectList(wrapper);
		ExcelUtil<ProductManagement> util = new ExcelUtil<ProductManagement>(ProductManagement.class);
		return util.exportExcel(list, "操作日志");
	}
}
