package com.pj.project.tb_favorite_contacts;

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

import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.project.tb_favorite_contacts.param.FavoriteContactsParam;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.pj.utils.sg.*;

/**
 * Service: tb_favorite_contacts -- 常用联系人
 * @author lbl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbFavoriteContactsService extends ServiceImpl<TbFavoriteContactsMapper, TbFavoriteContacts> implements IService<TbFavoriteContacts>{

	/** 底层 Mapper 对象 */
	@Autowired
	TbFavoriteContactsMapper tbFavoriteContactsMapper;

	/** 增 */
	void add(TbFavoriteContacts t){
		save(t);
	}

	/** 增 */
	void appAdd(FavoriteContactsParam favoriteContactsParam){
		//获取当前登陆人
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
		if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
			throw new SecurityException("当前登录账号信息已失效!");
		TbFavoriteContacts t = new TbFavoriteContacts();
		BeanUtils.copyProperties(favoriteContactsParam,t);
		t.setFkAppUser(appLoginInfo.getFk());
		//设置公共字段
		t.setCreateTime(new Date());
		t.setCreateName(appLoginInfo.getLoginName());
		t.setCreateBy(appLoginInfo.getLoginId());
		t.setDeleteStatus(DeleteStatus.DELETE_STATUS_ON.getCode());
		save(t);
	}

	/** 删 */
	void delete(Long id){
		removeById(id);
	}

	/** 改 */
	void update(TbFavoriteContacts t){
		updateById(t);

	}

	/** 查 */
	TbFavoriteContacts getById(Long id){
		return super.getById(id);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */
	List<TbFavoriteContacts> getList(SoMap so) {
		return tbFavoriteContactsMapper.getList(so);
	}
	/** 查集合 - 查询自己的装货地址 */
	List<TbFavoriteContacts> getAppList(SoMap so) {
		//获取当前登录人
		APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
		if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
			throw new SecurityException("当前登录账号信息已失效!");
		//创建查询条件
		so.put("fkAppUser",appLoginInfo.getFk());
		so.put("deleteStatus", DeleteStatus.DELETE_STATUS_ON.getCode());
		return tbFavoriteContactsMapper.getList(so);
	}


}
