package com.railway.business.catenary.service.impl;

import com.github.pagehelper.Page;
import com.railway.business.baseinfo.service.IBaseStationService;
import com.railway.business.catenary.domain.BusJcbGlkg;
import com.railway.business.catenary.domain.vo.JcebCountVo;
import com.railway.business.catenary.domain.vo.JcebQueryVo;
import com.railway.business.catenary.domain.vo.JcebRecordVo;
import com.railway.business.catenary.enums.JcebStatusEnum;
import com.railway.business.catenary.mapper.BusJcbGlkgMapper;
import com.railway.business.catenary.service.IBusJcbGlkgService;
import com.railway.common.utils.StringUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.railway.common.utils.poi.EasyExcelUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 隔离开关检查表
* @author author
* @date 2021/10/13
*/
@Service
public class BusJcbGlkgServiceImpl extends BusJcabServiceImpl implements IBusJcbGlkgService{

	private final BusJcbGlkgMapper busJcbGlkgMapper;

	public BusJcbGlkgServiceImpl(BusJcbGlkgMapper busJcbGlkgMapper,
			IBaseStationService stationService) {
    super(stationService);
    this.busJcbGlkgMapper = busJcbGlkgMapper;
	}

	/**
	* 新增
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int create(BusJcbGlkg busJcbGlkg) {
		setCreate(busJcbGlkg);
		busJcbGlkg.setUpdateTime(new Date());
		return busJcbGlkgMapper.insert(busJcbGlkg);
	}

	/**
	 * 提交
	 * @param busJcbGlkg 数据对象
	 * @return 提交结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int submit(BusJcbGlkg busJcbGlkg) {
		busJcbGlkg.setSubmitState(JcebStatusEnum.REPORTED.getCode());
		if(StringUtils.isEmpty(busJcbGlkg.getId())){
			return create(busJcbGlkg);
		}else {
			return update(busJcbGlkg);
		}
	}

	/**
	 * 退回
	 * @param busJcbGlkg 数据对象
	 * @return 退回结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int reject(BusJcbGlkg busJcbGlkg) {
		setReject(busJcbGlkg);
		return update(busJcbGlkg);
	}

	/**
	 * 确认
	 * @param busJcbGlkg 数据对象
	 * @return 确认结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int confirm(BusJcbGlkg busJcbGlkg) {
		setConfirm(busJcbGlkg);
		return update(busJcbGlkg);
	}

	/**
	* 删除
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(String[] ids) {
		int r =0;
		for (String id : ids) {
			int j= busJcbGlkgMapper.delete(id);
			r = r + j;
		}
		return	r;
	}

	/**
	* 更新
	*/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int update(BusJcbGlkg busJcbGlkg) {
		setUpdate(busJcbGlkg);
		return busJcbGlkgMapper.update(busJcbGlkg);
	}

	/**
	* 获取单个
	*/
	@Override
	public BusJcbGlkg getInfo(String id) {
		return busJcbGlkgMapper.getInfo(id);
	}

	@Override
	public BusJcbGlkg getJcebInfo(String id) {
		return formatOne(busJcbGlkgMapper.getInfo(id));
	}

	/**
	* 查询列表
	*/
	@Override
	public List<BusJcbGlkg> getList(BusJcbGlkg busJcbGlkg) {
		setList(busJcbGlkg);
		return busJcbGlkgMapper.getList(busJcbGlkg);
	}

	@Override
	public BusJcbGlkg getLastOne(BusJcbGlkg busJcbGlkg) {
		return busJcbGlkgMapper.getLastOne(busJcbGlkg);
	}

	/**
	 * 查询明细列表
	 * @param query 查询参数
	 * @return 数据记录明细
	 */
	@Override
	public Page<JcebRecordVo> getListByType(JcebQueryVo query) {
		setJcebQuery(query);
		return busJcbGlkgMapper.getListByType(query);
	}

	/**
	 * 查询汇总列表
	 * @param query 查询参数
	 * @return 汇总数据
	 */
	@Override
	public JcebCountVo getCountByType(JcebQueryVo query) {
		setJcebQuery(query);
		return busJcbGlkgMapper.getCountByType(query);
	}

	@Override
	public String exportExcel(JcebQueryVo query) {
		setJcebQuery(query);
		List<BusJcbGlkg> list = busJcbGlkgMapper.getJcebList(query);
		list = list.stream().map(x -> formatOne(x)).collect(Collectors.toList());
		String fileName = "隔离开关检测记录.xls";
		String templateFileName = "excel/template/隔离开关检测记录.xls";
		return new EasyExcelUtil().writeExcel(fileName, templateFileName, list);
	}

	private BusJcbGlkg formatOne(BusJcbGlkg busJcbGlkg) {
			busJcbGlkg.setZcjyzztA(StringUtils.goodOrBad(busJcbGlkg.getZcjyzztA()));
			busJcbGlkg.setYdfhsyA(StringUtils.goodOrBad(busJcbGlkg.getYdfhsyA()));
			busJcbGlkg.setDdfhsyA(StringUtils.goodOrBad(busJcbGlkg.getDdfhsyA()));
			busJcbGlkg.setSdfhsyA(StringUtils.goodOrBad(busJcbGlkg.getSdfhsyA()));
			busJcbGlkg.setXiangbie("检测");
			// 开关引线状态
			if (StringUtils.YES_VALUE.equals(busJcbGlkg.getYxztA())
				&& StringUtils.YES_VALUE.equals(busJcbGlkg.getYxztB())
				&& StringUtils.YES_VALUE.equals(busJcbGlkg.getYxztC())) {
				busJcbGlkg.setYxztA(StringUtils.goodOrBad(StringUtils.YES_VALUE));
			}
			else {
				busJcbGlkg.setYxztA(StringUtils.goodOrBad(StringUtils.NO_VALUE));
			}

			// 操作机构
			if (StringUtils.YES_VALUE.equals(busJcbGlkg.getQdzzA())
				&& StringUtils.YES_VALUE.equals(busJcbGlkg.getJgxA())
				&& StringUtils.YES_VALUE.equals(busJcbGlkg.getJgxB())
				&& StringUtils.YES_VALUE.equals(busJcbGlkg.getJgxC())) {
				busJcbGlkg.setJgxA(StringUtils.goodOrBad(StringUtils.YES_VALUE));
			}
			else {
				busJcbGlkg.setJgxA(StringUtils.goodOrBad(StringUtils.NO_VALUE));
			}

			busJcbGlkg.setFzzt(StringUtils.goodOrBad(busJcbGlkg.getFzzt()));
			busJcbGlkg.setHzzt(StringUtils.goodOrBad(busJcbGlkg.getHzzt()));

			return busJcbGlkg;
	}

}
