package com.wechat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wechat.common.utils.JacksonUtils;
import com.wechat.dao.MdeJobAdvertisedDao;
import com.wechat.dao.MendunerShowDao;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeShowInfo;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dbEntity.MdeVipPackage;
import com.wechat.model.dto.ShowOfJobsDto;
import com.wechat.model.dto.ShowOfUserDto;
import com.wechat.model.requestDto.MdeShowReq;
import com.wechat.model.responseDto.MdeShowListResp;
import com.wechat.model.responseDto.MdeShowResp;
import com.wechat.model.responseDto.PhotoAlbumResp;

/**
* @author jadyn
* @version 创建时间：2019年6月4日 下午6:13:31
*  类说明
*/
@Service
@Transactional
public class MendunerShowService {
	
	@Autowired
	private MendunerShowDao mdeShowDao;
	
	@Autowired
	private MdeJobAdvertisedDao jobAdvertisedDao;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private PersonCeneterService personCenterService;
	
	/**
	 * 创建、修改门墩儿秀
	 * @param userCode
	 * @param showReq
	 * @return
	 */
	public int makeMdeShow(String userCode, MdeShowReq showReq) {
		MdeShowInfo dbEntity = new MdeShowInfo();
		//将入参和db类中相同属性复制至db中
		BeanUtils.copyProperties(showReq, dbEntity);
		//差异化参数封装
		dbEntity.setUserCode(userCode);
		dbEntity.setWelfares(JacksonUtils.list2json(showReq.getWelfaresList()));
		dbEntity.setJobIds(JacksonUtils.list2json(showReq.getJobIdsList()));
		int count = 0;
		if(showReq.getShowId() == null)
		{
			//新增
			count = mdeShowDao.newMdeShow(dbEntity);
		} else {
			//修改
			count = mdeShowDao.updMdeShow(dbEntity);
		}
		int showId = dbEntity.getShowId();
		if(count < 1)
		{
			showId = 0;
		}
		return showId;
	}
	
	/**
	 * 获取秀的创建信息
	 * @param userCode
	 * @param showReq
	 * @return
	 */
	public MdeShowReq getInfoAboutShow(String userCode, int showId) {
		MdeShowReq showReq = new MdeShowReq();
		//获取数据
		MdeShowInfo dbEntity = mdeShowDao.getInfoForUpd(userCode,showId);
		//将b类中相同属性复制至返回结果集中
		BeanUtils.copyProperties(dbEntity,showReq);
		showReq.setWelfaresList(JacksonUtils.json2list(dbEntity.getWelfares(),String.class));
		showReq.setJobIdsList(JacksonUtils.json2list(dbEntity.getJobIds(),String.class));
		
		return showReq;
	}

	
	/**
	 * 删除门墩儿秀
	 * @param userCode
	 * @param showReq
	 * @return
	 */
	public int delShow(String userCode, int showId) {
		return  mdeShowDao.delShow(userCode,showId);
	}
	
	
	/**
	 * 获取秀的列表
	 * @param userCode
	 * @return
	 */
	public List<MdeShowListResp> getShowList(String userCode) {
		//获取数据
		List<MdeShowListResp> showList = mdeShowDao.getShowList(userCode);
		
		return showList;
	}
	
	
	/**
	 * 获取秀的展示信息
	 * @param userCode
	 * @param showId
	 * @return
	 */
	public MdeShowResp getTheShow(int showId) {
		MdeShowResp showResp = new MdeShowResp();
		//获取秀的信息数据
		MdeShowInfo showBase = mdeShowDao.getInfoById(showId);
		if(showBase != null)
		{
			//获取用户基础数据
			MdeUser user = commonService.getUserInfo(showBase.getUserCode());
			ShowOfUserDto showOfUser = new ShowOfUserDto();
			BeanUtils.copyProperties(user,showOfUser);
			showOfUser.setWelfaresList(JacksonUtils.json2list(showBase.getWelfares(),String.class));
			
			//获取企业相册
			List<PhotoAlbumResp> photoAlbumList = personCenterService.getPhotoAlbum(showBase.getUserCode());

			//根据jobid 获取职位详情
			List<ShowOfJobsDto> showOfJobsList = jobAdvertisedDao.getInfoByIds(JacksonUtils.json2list(showBase.getJobIds(),String.class));

			//结果集封装
			BeanUtils.copyProperties(showBase,showResp);
			showResp.setShowOfUser(showOfUser);
			showResp.setShowOfJobsList(showOfJobsList);
			showResp.setPhotoAlbumList(photoAlbumList);
		}

		return showResp;
	}
	
}
