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

import java.util.List;

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.mapper.shop.ShopTranMapper;
import com.iotechn.unimall.data.domain.shop.ShopTran;
import com.iotechn.unimall.admin.api.shop.IShopTranService;
import com.iotechn.unimall.data.model.Page;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物流公司Service业务层处理
 * 
 * @author jlb
 * @date 2022-05-29
 */
@Service
public class ShopTranServiceImpl implements IShopTranService{
	@Autowired
	private ShopTranMapper shopTranMapper;
	
	@Override
	public Boolean add(ShopTran shopTran,Long adminId) throws ServiceException {
		Date now = new Date();
		shopTran.setGmtCreate(now);
		shopTran.setGmtUpdate(now);
		shopTran.setAdminId(adminId);
		return shopTranMapper.insert(shopTran)>0;
	}

	@Override
	public Page<ShopTran> list(String name,String code1,String code2,String phone,Integer deleteFlag,Date gmtCreate,Date gmtUpdate,Long userId,Long adminId, Integer page, Integer limit)throws ServiceException {
		Wrapper<ShopTran> wrapper = new EntityWrapper<ShopTran>();
														if (!StringUtils.isEmpty(name)) {
					wrapper.eq("name", name);
				}
												if (!StringUtils.isEmpty(code1)) {
					wrapper.eq("code1", code1);
				}
												if (!StringUtils.isEmpty(code2)) {
					wrapper.eq("code2", code2);
				}
												if (!StringUtils.isEmpty(phone)) {
					wrapper.eq("phone", phone);
				}
																	if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(userId)) {
					wrapper.eq("user_id", userId);
				}
												if (!StringUtils.isEmpty(adminId)) {
					wrapper.eq("admin_id", adminId);
				}
							wrapper.eq("delete_flag", 0);
		List<ShopTran> list = shopTranMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
		Integer count = shopTranMapper.selectCount(wrapper);
		return new Page<ShopTran>(list, page, limit, count);
	}

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

	@Override
	public Boolean update(ShopTran shopTran,Long adminId) throws ServiceException {
		Date now = new Date();
		shopTran.setGmtUpdate(now);
		shopTran.setAdminId(adminId);
		return shopTranMapper.updateById(shopTran)>0;
	}

	@Override
	public ShopTran get(Long id) throws ServiceException {
		return shopTranMapper.selectById(id);
	}
	
	@Override
	public String export(String name,String code1,String code2,String phone,Integer deleteFlag,Date gmtCreate,Date gmtUpdate,Long userId,Long adminId, Integer page, Integer limit)throws ServiceException {
		Wrapper<ShopTran> wrapper = new EntityWrapper<ShopTran>();
														if (!StringUtils.isEmpty(name)) {
					wrapper.eq("name", name);
				}
												if (!StringUtils.isEmpty(code1)) {
					wrapper.eq("code1", code1);
				}
												if (!StringUtils.isEmpty(code2)) {
					wrapper.eq("code2", code2);
				}
												if (!StringUtils.isEmpty(phone)) {
					wrapper.eq("phone", phone);
				}
																	if (!StringUtils.isEmpty(deleteFlag)) {
					wrapper.eq("delete_flag", deleteFlag);
				}
												if (!StringUtils.isEmpty(gmtCreate)) {
					wrapper.eq("gmt_create", gmtCreate);
				}
												if (!StringUtils.isEmpty(gmtUpdate)) {
					wrapper.eq("gmt_update", gmtUpdate);
				}
												if (!StringUtils.isEmpty(userId)) {
					wrapper.eq("user_id", userId);
				}
												if (!StringUtils.isEmpty(adminId)) {
					wrapper.eq("admin_id", adminId);
				}
							List<ShopTran> list = shopTranMapper.selectList(wrapper);
		ExcelUtil<ShopTran> util = new ExcelUtil<ShopTran>(ShopTran.class);
		return util.exportExcel(list, "操作日志");
	}
}
