package com.ozs.appshow.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ozs.appshow.service.SubjectManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ozs.appshow.dto.ListSubjectDTO;
import com.ozs.appshow.dto.RelSubjectChartDTO;
import com.ozs.appshow.dto.RelSubjectChartParam;
import com.ozs.appshow.dto.SubjectInfoDTO;
import com.ozs.appshow.dto.SubjectPageInfoDTO;
import com.ozs.appshow.mapper.SubjectManageMapper;
import com.ozs.appshow.util.R;

@Service
public class SubjectManageServiceImpl implements SubjectManageService {
	@Autowired
	private SubjectManageMapper SubjectManageMapper;

	@Override
	public R<ListSubjectDTO> listSubject(String subjectName) {
		ListSubjectDTO ListSubject = new ListSubjectDTO();
		// 上线主题列表
		List<SubjectInfoDTO> subjectListUp = SubjectManageMapper.listSubjectByState(subjectName, 1);
		// 下线主题列表
		List<SubjectInfoDTO> subjectListDown = SubjectManageMapper.listSubjectByState(subjectName, 0);
		ListSubject.setSubjectListDown(subjectListDown);
		ListSubject.setSubjectListUp(subjectListUp);
		return new R<ListSubjectDTO>(ListSubject);
	}

	/**
	 * 保存专题
	 */
	@Transactional
	@Override
	public R<String> saveSubject(SubjectInfoDTO subjectInfo) {
		R<String> r = new R<>();
		// 默认上线
		subjectInfo.setState(1);
		SubjectInfoDTO subjectInfoDTO = SubjectManageMapper.getSubjectByName(subjectInfo.getSubjectName());
		// 专题名称判断重复
		if (subjectInfoDTO != null) {
			r.setCode("-1");
			r.setMsg("专题名称已存在");
			r.setData("专题名称已存在");
			return r;
		}
		SubjectManageMapper.saveSubject(subjectInfo);
		r.setMsg("保存成功");
		r.setData("保存成功");
		return r;
	}

	/**
	 * 修改专题
	 */
	@Transactional
	@Override
	public R<String> updateSubject(SubjectInfoDTO subjectInfo) {
		R<String> r = new R<>();
		SubjectInfoDTO subjectInfoDTO = SubjectManageMapper.getSubjectByName(subjectInfo.getSubjectName());
		// 专题名称判断重复
		if (subjectInfoDTO != null && !subjectInfoDTO.getId().equals(subjectInfo.getId())) {
			r.setCode("-1");
			r.setMsg("专题名称已存在");
			r.setData("专题名称已存在");
			return r;
		}
		SubjectManageMapper.updateSubject(subjectInfo);
		r.setMsg("修改成功");
		r.setData("修改成功");
		return r;
	}

	/**
	 * 删除专题
	 */
	@Transactional
	@Override
	public R<String> removeSubject(String id) {
		R<String> r = new R<>();
		// 删除专题
		SubjectManageMapper.removeSubject(id);
		// 删除专题图表关联信息
		SubjectManageMapper.removeRelSubjectChart(id);

		r.setMsg("删除成功");
		r.setData("删除成功");
		return r;
	}

	/**
	 * 修改专题状态
	 */
	@Transactional
	@Override
	public R<String> updateSubjectState(SubjectInfoDTO subjectInfo) {
		R<String> r = new R<>();
		// SubjectInfoDTO subjectInfoDTO =
		// SubjectManageMapper.getSubjectById(subjectInfo.getId());
		SubjectManageMapper.updateSubjectState(subjectInfo);
		r.setMsg("修改成功");
		r.setData("修改成功");
		return r;
	}

	/**
	 * 配置专题图表关联
	 */
	@Transactional
	@Override
	public R<String> saveRelSubjectChart(RelSubjectChartParam param) {
		R<String> r = new R<>();
		List<RelSubjectChartDTO> relChartList = new ArrayList<>();

		List<SubjectPageInfoDTO> subjectPages = param.getChildren();
		for (SubjectPageInfoDTO subjectPage : subjectPages) {
			relChartList.addAll(subjectPage.getChildren());
			subjectPage.setSubjectId(param.getSubjectId());
			subjectPage.setSubjectPage(subjectPages.indexOf(subjectPage) + 1);

			List<RelSubjectChartDTO> list = subjectPage.getChildren();
			for (RelSubjectChartDTO c : list) {
				c.setSubjectPage(subjectPage.getSubjectPage());
				c.setSubjectId(param.getSubjectId());
			}
		}
		// 保存专题每页信息
		if (subjectPages != null && subjectPages.size() != 0) {
			SubjectManageMapper.saveSubjectPage(subjectPages);
		}
		// 保存专题图表关联信息
		if (relChartList != null && relChartList.size() != 0) {
			SubjectManageMapper.saveRelSubjectChart(relChartList);
		}
		r.setMsg("保存成功");
		r.setData("保存成功");
		return r;
	}

	/**
	 * 获取专题信息
	 */
	@Override
	public R<RelSubjectChartParam> getSubject(String id) {
		R<RelSubjectChartParam> r = new R<>();
		RelSubjectChartParam chartParam = new RelSubjectChartParam();
		SubjectInfoDTO subjectInfo = SubjectManageMapper.getSubjectById(id);
		chartParam.setSubjectId(subjectInfo.getId());
		chartParam.setSubjectName(subjectInfo.getSubjectName());
		// 获取每页专题信息
		List<SubjectPageInfoDTO> subjectPages = SubjectManageMapper.listSubjectPageInfo(id);
		// 专题图表关联信息
		List<RelSubjectChartDTO> relChartList = SubjectManageMapper.listRelSubjectChart(id);
		for (SubjectPageInfoDTO page : subjectPages) {
			List<RelSubjectChartDTO> children = new ArrayList<>();
			for (RelSubjectChartDTO chart : relChartList) {
				if (chart.getSubjectPage() == page.getSubjectPage()) {
					children.add(chart);
				}
			}
			page.setChildren(children);
		}
		chartParam.setChildren(subjectPages);
		r.setData(chartParam);
		return r;
	}

}
