package cn.com.countrygarden.bi.main.service.advise.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.advise.AfAdviseFeedbackMapper;
import cn.com.countrygarden.bi.main.dao.advise.AfAdviseNoticeMapper;
import cn.com.countrygarden.bi.main.dto.PageDataVo;
import cn.com.countrygarden.bi.main.dto.advise.param.AdviseFeedbackParam;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseFeedback;
import cn.com.countrygarden.bi.main.model.advise.AfAdviseNotice;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseBranchTypeService;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseNoticeService;
import cn.com.countrygarden.bi.main.service.advise.IAfAdviseResponsiblerService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

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

/**
 * 公告建言-业务逻辑层实现
 *
 * @author 木木子
 * @email 944528607@qq.com
 * @url
 * @date 2019-04-15
 */
@Service
@Transactional
public class AfAdviseNoticeServiceImpl implements IAfAdviseNoticeService {
	@Autowired
	private AfAdviseNoticeMapper afAdviseNoticeMapper;
	@Autowired
	private AfAdviseFeedbackMapper afAdviseFeedbackMapper;
	@Autowired
	private IAfAdviseResponsiblerService afAdviseResponsiblerService;
	@Autowired
	private IAfAdviseBranchTypeService afAdviseBranchTypeService;
	@Override
	public Result queryPageList(AdviseFeedbackParam adviseFeedbackParam, int page, int size) {
		if(null==afAdviseBranchTypeService.getUserAdviseBranchRoleCode(null,adviseFeedbackParam.getUserCode())){
			Result result= new Result();
			result.setResultMsg("你不属于建言系统的任何分支类型，操作失败");
			result.setData("noRole");
			result.setResultCode(500);
			return result;
		}
		adviseFeedbackParam=afAdviseResponsiblerService.getAdviseResponsiblerDTO(adviseFeedbackParam);
		Page<Object> pageList = PageHelper.startPage(page, size);

		if(null!=adviseFeedbackParam.getRole()&&adviseFeedbackParam.getRole()){
			//如果是超级管理员，不需要对数据进行过滤
			if(null!=adviseFeedbackParam.getSuperAdminRole()&&adviseFeedbackParam.getSuperAdminRole()){
				adviseFeedbackParam.setRegionAdminRole(null);
				//如果是类型管理员，不需要对数据进行过滤
			}else if(null!=adviseFeedbackParam.getTypeAdminRole()&&adviseFeedbackParam.getTypeAdminRole()){
				adviseFeedbackParam.setRegionAdminRole(null);
				//如果是总部管理员，不需要对数据进行过滤
			}else if(null!=adviseFeedbackParam.getHeadquartersAdminRole()&&adviseFeedbackParam.getHeadquartersAdminRole()){
				adviseFeedbackParam.setRegionAdminRole(null);
			}
		}

		adviseFeedbackParam.setBranchTypeCode(StringUtil.isNotBlank(adviseFeedbackParam.getBranchTypeCode()) ? adviseFeedbackParam.getBranchTypeCode() : "advise_juzhele");
		List<AfAdviseNotice> adviseNoticeList = afAdviseNoticeMapper.selectAdviseNoticeList(adviseFeedbackParam);
		PageDataVo data = new PageDataVo(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);
	}

	@Override
	public Result queryById(Integer id) {
		return Response.response(afAdviseNoticeMapper.selectByPrimaryKey(id));
	}

	@Override
	public Result del(Integer id) {
		int count = afAdviseNoticeMapper.deleteByPrimaryKey(id);
		return Response.response(count);
	}

	@Override
	public Result addOrEdit(AfAdviseNotice afAdviseNotice, AccessToken accessToken) {
     int afAdviseNoticeState=afAdviseNotice.getState();
		Condition condition=new Condition(AfAdviseNotice.class);
		condition.createCriteria().andEqualTo("adviseFeedbackId",afAdviseNotice.getAdviseFeedbackId());
	   int count=	afAdviseNoticeMapper.selectCountByCondition(condition);
		afAdviseNotice.setPublishUserCode(accessToken.getUserCode());
		afAdviseNotice.setPublishTime(new Date());
		// 发布、撤回公告
		if (count>0) {
			 count = afAdviseNoticeMapper.updateByConditionSelective(afAdviseNotice,condition);
			// 新增公告
		} else {
			AfAdviseFeedback adviseFeedback = new AfAdviseFeedback();
			adviseFeedback.setId(afAdviseNotice.getAdviseFeedbackId());
			adviseFeedback.setEnable(1);
			//建言完成时才可被发布为公告，所以state=2
			adviseFeedback.setState(2);
			adviseFeedback = afAdviseFeedbackMapper.selectOne(adviseFeedback);
			BeanUtils.copyProperties(adviseFeedback, afAdviseNotice);
			//公告状态（0保存1发布2撤回）
			afAdviseNotice.setId(null);
			afAdviseNotice.setState(afAdviseNoticeState);
			afAdviseNotice.setReportUserCode(adviseFeedback.getCreateBy());
			 count = afAdviseNoticeMapper.insertSelective(afAdviseNotice);
		}
		return Response.response(count);
	}

}