package org.springblade.modules.csInfo.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.appmenu.vo.DbsxVO;
import org.springblade.modules.appmenu.vo.SssjVO;
import org.springblade.modules.csInfo.dto.TimeDTO;
import org.springblade.modules.csInfo.entity.CsInfoEntity;
import org.springblade.modules.csInfo.entity.CsRlb;
import org.springblade.modules.csInfo.excel.*;
import org.springblade.modules.csInfo.mapper.CsInfoMapper;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.csInfo.vo.CsInfoVO;
import org.springblade.modules.csInfo.vo.CsInfoVOS;
import org.springblade.modules.csInfo.vo.InfoVO;
import org.springblade.modules.csInfo.vo.TjfxVO;
import org.springblade.modules.sqjw.jwtdzrq.entity.JwtdZrqEntity;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.mapper.DeptMapper;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.vo.DeptAllVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 场所 服务实现类
 *
 * @author BladeX
 * @since 2023-07-21
 */
@Service
public class CsInfoServiceImpl extends BaseServiceImpl<CsInfoMapper, CsInfoEntity> implements ICsInfoService {

	@Autowired
	private IDeptService deptService;
	@Autowired
	private CsInfoJlServiceImpl csInfoJlService;
	@Autowired
	private DeptMapper deptMapper;

	@Override
	public IPage<CsInfoVO> selectInfoPage(IPage<CsInfoVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.selectInfoPage(page, params));
	}

	@Override
	public IPage<CsInfoVO> selectInfoPagePublic(IPage<CsInfoVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.selectInfoPagePublic(page, params));
	}

	@Override
	public CsInfoEntity selectCsInfoByIdAndPcsbm(Map<String, Object> param) {
		return baseMapper.selectCsInfoByIdAndPcsbm(param);
	}


	@Override
	public List<CsInfoExcel> exportInfo(Map<String, Object> params) {
		List<CsInfoExcel> infoList = baseMapper.exportInfo(params);
		return infoList;
	}

	@Override
	public CsInfoVO getDetail(Long id) {
		return baseMapper.getDetail(id);
	}

	@Override
	public int getOverDue(Map<String, Object> params) {
		return baseMapper.getOverDue(params);
	}

	@Override
	public int getXqzgTx(Map<String, Object> params) {
		return baseMapper.getXqzgTx(params);
	}

	@Override
	public IPage<CsInfoEntity> selectCsPcs(IPage<Object> page, CsInfoEntity csInfo, String deptId) {
		String str = "";
		String strEnd = "";
		if (csInfo.getDatetime() != null) {
			String[] datetime = csInfo.getDatetime().split(",");
			str = datetime[0];
			strEnd = datetime[1];
		}
		return baseMapper.selectCsPcs(page, csInfo, deptId, str, strEnd);
	}

	@Override
	public List<CsInfoVOS> selectCsSq(Long id, String datetime) {
		String str = "";
		String strEnd = "";
		if (datetime != null && datetime != "") {
			String[] datetimes = datetime.split(",");
			str = datetimes[0];
			strEnd = datetimes[1];
		}
		return baseMapper.selectCsSq(id, str, strEnd);
	}

	@Override
	public IPage<CsInfoEntity> selectCsxx(IPage<Object> page, CsInfoEntity csInfo) {
		return baseMapper.selectCsxx(page, csInfo);
	}

	@Override
	public IPage<CsInfoEntity> selectSqByDeptname(IPage<Object> page, CsInfoEntity csInfo, String deptId) {
		String str = "";
		String strEnd = "";
		if (csInfo.getDatetime() != null) {
			String[] datetime = csInfo.getDatetime().split(",");
			str = datetime[0];
			strEnd = datetime[1];
		}
		return baseMapper.selectSqByDeptname(page, csInfo, deptId, str, strEnd);
	}

	@Override
	public DeptAllVO getParentInfo(Long sssqBm) {
		return baseMapper.getParentInfo(sssqBm);
	}

	@Override
	public List<Dept> getDeptList(Map<String, Object> params) {
		return baseMapper.getDeptList(params);
	}

	@Override
	public IPage<QsJxcsExcel> getQsJxcs(IPage<QsJxcsExcel> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getQsJxcs(page, params));
	}

	@Override
	public List<QsJxcsExcel> exportQsJxcs(Map<String, Object> params) {
		return baseMapper.exportQsJxcs(params);
	}

	@Override
	public IPage<MjsjtjExcel> getSqTj(IPage<MjsjtjExcel> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getSqTj(page, params));
	}

	@Override
	public IPage<MjsjtjExcel> getPcsTj(IPage<MjsjtjExcel> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getPcsTj(page, params));
	}

	@Override
	public IPage<MjsjtjExcel> getFjTj(IPage<MjsjtjExcel> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getFjTj(page, params));
	}

	@Override
	public IPage<MjsjtjExcel> getQsTj(IPage<MjsjtjExcel> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getQsTj(page, params));
	}

	@Override
	public List<MjsjtjExcel> exportSqTj(Map<String, Object> params) {
		return baseMapper.exportSqTj(params);
	}

	@Override
	public List<MjsjtjExcel> exportPcsTj(Map<String, Object> params) {
		return baseMapper.exportPcsTj(params);
	}

	@Override
	public List<MjsjtjExcel> exportFjTj(Map<String, Object> params) {
		return baseMapper.exportFjTj(params);
	}

	@Override
	public List<MjsjtjExcel> exportQsTj(Map<String, Object> params) {
		return baseMapper.exportQsTj(params);
	}

	@Override
	public List<TimeDTO> getXfjcTime(Map<String, Object> params) {
		return baseMapper.getXfjcTime(params);
	}

	@Override
	public SssjVO getSssj(Map<String, Object> params) {
		String startTime = "";
		String endTime = "";
		if (params.get("startTime") != null && params.get("endTime")!= null) {
			startTime = params.get("startTime").toString();
			endTime = params.get("endTime").toString();
		}
		params.put("roleName", AuthUtil.getUserRole());
		SssjVO sssjVO = new SssjVO();
		SssjVO jxcs = baseMapper.getJxcs(params, startTime, endTime);
		SssjVO xfjc = baseMapper.getXfjc(params, startTime, endTime);
		SssjVO mdjf = baseMapper.getMdjf(params, startTime, endTime);
		SssjVO rhzf = baseMapper.getRhzf(params, startTime, endTime);
		SssjVO rhzfrs = baseMapper.getRhzfrs(params, startTime, endTime);
		sssjVO.setJxcs(jxcs.getJxcs());
		sssjVO.setXfjc(xfjc.getXfjc());
		sssjVO.setMdjf(mdjf.getMdjf());
		sssjVO.setRhzf(rhzf.getRhzf());
		sssjVO.setRhzfrs(rhzfrs.getRhzfrs());
		return sssjVO;
	}

	@Override
	public DbsxVO getDbsxVOjc(String deptId) {
		return baseMapper.getDbsxVOjc(deptId);
	}

	@Override
	public IPage<TjfxVO> getTjfxSjtj(IPage<TjfxVO> page, TjfxVO tjfxVO) {
		return getTj(page, "getTjfxSjtj", tjfxVO);
	}

	@Override
	public IPage<TjfxVO> getTjfxFjtj(IPage<TjfxVO> page, TjfxVO tjfxVO) {
		return getTj(page, "getTjfxFjtj", tjfxVO);
	}

	@Override
	public IPage<TjfxVO> getTjfxPcstj(IPage<TjfxVO> page, TjfxVO tjfxVO) {
		return getTj(page, "getTjfxPcstj", tjfxVO);
	}

	@Override
	public IPage<TjfxVO> getTjfxMjtj(IPage<TjfxVO> page, TjfxVO tjfxVO) {
		return getTj(page, "getTjfxMjtj", tjfxVO);
	}

	private IPage<TjfxVO> getTj(IPage<TjfxVO> page, String method, TjfxVO tjfxVO) {
		Dept id = new Dept();
		List<Dept> jl = deptMapper.getJl();
		for (Dept xh:jl){
			if (AuthUtil.getDeptId().equals(xh.getId().toString())){
				id = deptMapper.getName(AuthUtil.getDeptId());
				if (AuthUtil.getDeptId().equals("220206000000")) id.setDeptName("经开分局");

				if (AuthUtil.getDeptId().equals("220200000000")) id = deptMapper.getName(AuthUtil.getDeptId());

				Dept publics = deptMapper.getPublicName(id.getDeptName());

				if (id.getDeptCategory() == 4) tjfxVO.setJgdm(publics.getIdsj());

				if (id.getDeptCategory() == 5) tjfxVO.setJgdm(publics.getIdfj());

				if (id.getDeptCategory() == 6) tjfxVO.setJgdm(publics.getIdpcs());

				if (id.getDeptCategory() == 7) tjfxVO.setJgdm(publics.getIdsq());

				tjfxVO.setJglx(id.getDeptCategory());

				return returnTj(page, tjfxVO, method);
			}
		}
		tjfxVO.setJgdm(AuthUtil.getDeptId());
		Dept dept = deptService.getById(Long.parseLong(AuthUtil.getDeptId()));

		if(dept != null) tjfxVO.setJglx(dept.getDeptCategory());

		return returnTj(page, tjfxVO, method);
	}

	private IPage<TjfxVO> returnTj(IPage<TjfxVO> page, TjfxVO vo, String method) {

		if ("getTjfxSjtj".contains(method)) return baseMapper.getTjfxSjtj(page, vo);

		if ("getTjfxFjtj".contains(method)) return baseMapper.getTjfxFjtj(page, vo);

		if ("getTjfxPcstj".contains(method)) return baseMapper.getTjfxPcstj(page, vo);

		if ("getTjfxMjtj".contains(method)) return baseMapper.getTjfxMjtj(page, vo);

		return null;
	}

	@Override
	public List<TjfxSjExcel> exportTjfxSjtj() {
		return exportTj("exportTjfxSjtj");
	}

	@Override
	public List<TjfxFjExcel> exportTjfxFjtj() {
		return exportTj("exportTjfxFjtj");
	}

	@Override
	public List<TjfxPcsExcel> exportTjfxPcstj() {
		return exportTj("exportTjfxPcstj");
	}

	@Override
	public List<TjfxMjExcel> exportTjfxMjtj() {
		return exportTj("exportTjfxMjtj");
	}

	private List exportTj(String method) {
		TjfxVO tjfxVO = new TjfxVO();
		Dept id = new Dept();
		List<Dept> jl = deptMapper.getJl();
		for (Dept xh:jl){
			if (AuthUtil.getDeptId().equals(xh.getId().toString())){
				id = deptMapper.getName(AuthUtil.getDeptId());
				if (AuthUtil.getDeptId().equals("220206000000")) id.setDeptName("经开分局");

				if (AuthUtil.getDeptId().equals("220200000000")) id = deptMapper.getName(AuthUtil.getDeptId());

				Dept publics = deptMapper.getPublicName(id.getDeptName());

				if (id.getDeptCategory() == 4) tjfxVO.setJgdm(publics.getIdsj());

				if (id.getDeptCategory() == 5) tjfxVO.setJgdm(publics.getIdfj());

				if (id.getDeptCategory() == 6) tjfxVO.setJgdm(publics.getIdpcs());

				if (id.getDeptCategory() == 7) tjfxVO.setJgdm(publics.getIdsq());

				tjfxVO.setJglx(id.getDeptCategory());

				return returnTj(tjfxVO, method);
			}
		}
		tjfxVO.setJgdm(AuthUtil.getDeptId());
		Dept dept = deptService.getById(Long.parseLong(AuthUtil.getDeptId()));

		if(dept != null) tjfxVO.setJglx(dept.getDeptCategory());

		return returnTj(tjfxVO, method);
	}

	@Override
	public CsInfoEntity getPublicCsInfo(long id) {
		return baseMapper.getPublicCsInfo(id);
	}

	@Override
	public void addCsRl(CsRlb csRlb) {
		baseMapper.addCsRl(csRlb);
	}

	@Override
	@Transactional
	public boolean deleteByIds(String ids) {
		try {
			baseMapper.deleteByIds(ids);
			baseMapper.deleteRlIds(ids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public CsInfoEntity selectCsById(CsInfoEntity info) {
		return baseMapper.selectCsById(info);
	}

	@Override
	public List<CsRlb> getRlByCszjAndPcsbm(Map<String, Object> param) {
		return baseMapper.getRlByCszjAndPcsbm(param);
	}

	private List returnTj(TjfxVO vo, String method) {
		if ("exportTjfxSjtj".contains(method)) return baseMapper.exportTjfxSjtj(vo);

		if ("exportTjfxFjtj".contains(method)) return baseMapper.exportTjfxFjtj(vo);

		if ("exportTjfxPcstj".contains(method)) return baseMapper.exportTjfxPcstj(vo);

		if ("exportTjfxMjtj".contains(method)) return baseMapper.exportTjfxMjtj(vo);

		return null;
	}

	@Override
	public boolean getJxcsXf(CsInfoEntity csInfoEntity) {
		return baseMapper.getJxcsXf(csInfoEntity);
	}

	@Override
	public IPage<CsInfoEntity> getCsLb(IPage<CsInfoEntity> page, Long id, CsInfoEntity csInfoEntity) {
		return baseMapper.getCsLb(page, id, csInfoEntity);
	}

	@Override
	public List<CsInfoEntity> selectSb(String[] csIds) {
		return baseMapper.selectSb(csIds);
	}

	@Override
	public IPage<CsInfoVO> getJxcsSc(IPage<Object> page, Map<String, Object> params) {
		return baseMapper.getJxcsSc(page, params);
	}

	@Override
	public boolean getJxcsHf(String[] ids) {
		return baseMapper.getJxcsHf(ids);
	}

	@Override
	public CsInfoEntity selectShtyxydm(String tyxydm) {
		return baseMapper.selectShtyxydm(tyxydm);
	}

	@Override
	public boolean updateLastJcId(Long csId, Long jcId) {
		return baseMapper.updateLastJcId(csId, jcId);
	}

	@Override
	public List<Long> getLdList(Map<String, Object> params) {
		return baseMapper.getLdList(params);
	}

	@Override
	public boolean csLdGl(String[] ids, String ldphxlh) {
		return baseMapper.csLdGl(ids, ldphxlh);
	}

	@Override
	public boolean updateSspcs(String str, Long id, String deptName) {
		return baseMapper.updateSspcs(str, id, deptName);
	}

	@Override
	public boolean getQxgl(CsInfoEntity csInfoEntity) {
		return baseMapper.getQxgl(csInfoEntity);
	}

	@Override
	public IPage<InfoVO> getDztj(IPage<InfoVO> page, String deptId) {
		return baseMapper.getDztj(page, deptId);
	}

	@Override
	public IPage<CsInfoVO> getDzxq(IPage<CsInfoVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getDzxq(page, params));
	}

	@Override
	public List<DztjExcel> exportDztj(Map<String, Object> dztj) {
		return baseMapper.exportDztj(dztj);
	}

	@Override
	public int getJxcsSl(Long userId) {
		return baseMapper.getJxcsSl(userId);
	}

	@Override
	public JwtdZrqEntity getBySqBm(Long sssqbm) {
		return baseMapper.getBySqBm(sssqbm);
	}

	@Override
	public CsInfoEntity getTyxydm(String tyxydm) {
		return baseMapper.getTyxydm(tyxydm);
	}

	@Override
	public void deleteBySp(CsInfoEntity csInfoEntity) {
		baseMapper.deleteBySp(csInfoEntity);
	}

	@Override
	public void deleteBySpToJl(CsInfoEntity csInfoEntity) {
		baseMapper.deleteBySpToJl(csInfoEntity);
	}

	@Override
	public void hfDeleted(Long id) {
		baseMapper.hfDeleted(id);
	}

	@DS("jl")
	@Override
	public void saveToJl(CsInfoEntity info) {
		csInfoJlService.save(info);
	}

	@Override
	public void updateByIdToJl(CsInfoEntity csInfoEntity) {
		baseMapper.updateByIdToJl(csInfoEntity);
	}

	@Override
	public int countBySqBm(Map<String, Object> params) {
		return baseMapper.countBySqBm(params);
	}
}
