package com.xinelu.manage.service.propagandainfo.impl;

import com.xinelu.common.annotation.DataScope;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.agency.Agency;
import com.xinelu.manage.domain.materialsinfo.MaterialsInfo;
import com.xinelu.manage.domain.propagandainfo.PropagandaInfo;
import com.xinelu.manage.domain.propagandamaterials.PropagandaMaterials;
import com.xinelu.manage.dto.propagandainfo.PropagandaInfoDto;
import com.xinelu.manage.dto.propagandainfo.PropagandaInfoSaveDto;
import com.xinelu.manage.dto.propagandainfo.PropagandaUpdateDto;
import com.xinelu.manage.mapper.agency.AgencyMapper;
import com.xinelu.manage.mapper.materialsinfo.MaterialsInfoMapper;
import com.xinelu.manage.mapper.propagandainfo.PropagandaInfoMapper;
import com.xinelu.manage.mapper.propagandamaterials.PropagandaMaterialsMapper;
import com.xinelu.manage.service.propagandainfo.IPropagandaInfoService;
import com.xinelu.manage.vo.department.DepartmentBaseVo;
import com.xinelu.manage.vo.department.DepartmentVO;
import com.xinelu.manage.vo.propagandainfo.PropagandaMaterialsVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.xinelu.system.service.ISysConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 宣教库管理Service业务层处理
 *
 * @author haown
 * @date 2024-02-27
 */
@Service
public class PropagandaInfoServiceImpl implements IPropagandaInfoService {
    @Resource
    private PropagandaInfoMapper propagandaInfoMapper;
	@Resource
	private PropagandaMaterialsMapper propagandaMaterialsMapper;
	@Resource
	private MaterialsInfoMapper materialsInfoMapper;
	@Resource
	private AgencyMapper agencyMapper;
	@Resource
	private GenerateSystemCodeUtil generateSystemCodeUtil;
	@Resource
	private ISysConfigService sysConfigService;

    /**
     * 查询宣教库管理
     *
     * @param id 宣教库管理主键
     * @return 宣教库管理
     */
    @Override
    public PropagandaMaterialsVo selectPropagandaInfoById(Long id) {
	    PropagandaMaterialsVo retObj = new PropagandaMaterialsVo();
		PropagandaInfo propagandaInfo = propagandaInfoMapper.selectPropagandaInfoById(id);
		if (ObjectUtils.isEmpty(propagandaInfo)) {
			throw new ServiceException("数据异常");
		}
		BeanUtils.copyBeanProp(retObj, propagandaInfo);
		// 查询宣教素材管理关系表
	    PropagandaMaterials pmQuery = new PropagandaMaterials();
	    pmQuery.setPropagandaId(id);
	    List<PropagandaMaterials> pmList = propagandaMaterialsMapper.selectPropagandaMaterialsList(pmQuery);
	    List<MaterialsInfo> materialsInfoList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(pmList)) {
			// 查询素材列表
			for (PropagandaMaterials pm : pmList) {
				materialsInfoList.add(materialsInfoMapper.selectMaterialsInfoById(pm.getMaterialsId()));
			}
		}
	    retObj.setMaterialsInfoList(materialsInfoList);
	    return retObj;
    }

	@Override public PropagandaInfo getById(Long id) {
		return propagandaInfoMapper.selectPropagandaInfoById(id);
	}

	/**
     * 查询宣教库管理列表
     *
     * @param propagandaInfo 宣教库管理
     * @return 宣教库管理
     */
    @Override
    //@DataScope(agencyAlias = "p") 除admin其他账号的机构id必传
    public List<PropagandaInfo> selectPropagandaInfoList(PropagandaInfoDto propagandaInfo) {
		if (StringUtils.isNotBlank(propagandaInfo.getSource()) && propagandaInfo.getSource().equals("PUBLIC")){
			String publicAgencyId = sysConfigService.selectConfigByKey("public_agency_id");
			if (StringUtils.isNotBlank(publicAgencyId)){
				propagandaInfo.setPublicId(Long.parseLong(publicAgencyId));
			}
		}
        return propagandaInfoMapper.selectPropagandaInfoList(propagandaInfo);
    }

    /**
     * 新增宣教库管理
     *
     * @param propagandaInfoSaveDto 宣教库保存传输对象
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPropagandaInfo(PropagandaInfoSaveDto propagandaInfoSaveDto) {
		PropagandaInfo propagandaInfo = new PropagandaInfo();
	    BeanUtils.copyBeanProp(propagandaInfo, propagandaInfoSaveDto);
	    propagandaInfo.setPropagandaCode(Constants.PROPAGANDA_INFO_CODE + generateSystemCodeUtil.generateSystemCode(Constants.PROPAGANDA_INFO_CODE));
		propagandaInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        propagandaInfo.setCreateTime(LocalDateTime.now());
	    propagandaInfo.setDelFlag(0);
        int flag = propagandaInfoMapper.insertPropagandaInfo(propagandaInfo);
		if (flag > 0) {
			// 保存宣教素材关联关系
			if (CollectionUtils.isNotEmpty(propagandaInfoSaveDto.getMaterialsInfoList())) {
				for (MaterialsInfo materialsInfo : propagandaInfoSaveDto.getMaterialsInfoList()) {
					PropagandaMaterials propagandaMaterials = new PropagandaMaterials();
					propagandaMaterials.setPropagandaId(propagandaInfo.getId());
					propagandaMaterials.setPropagandaTitle(propagandaInfo.getPropagandaTitle());
					propagandaMaterials.setMaterialsId(materialsInfo.getId());
					propagandaMaterials.setMaterialsName(materialsInfo.getMaterialsName());
					propagandaMaterials.setCreateTime(LocalDateTime.now());
					propagandaMaterials.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
					propagandaMaterials.setDelFlag(0);
					propagandaMaterialsMapper.insertPropagandaMaterials(propagandaMaterials);
				}
			}
		}
		return flag;
    }

    /**
     * 修改宣教库管理
     *
     * @param propagandaInfoSaveDto 宣教库管理
     * @return 结果
     */
    @Override
    public int updatePropagandaInfo(PropagandaInfoSaveDto propagandaInfoSaveDto) {
	    PropagandaInfo propagandaInfo = new PropagandaInfo();
	    BeanUtils.copyBeanProp(propagandaInfo, propagandaInfoSaveDto);
	    propagandaInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        propagandaInfo.setUpdateTime(LocalDateTime.now());
        int flag = propagandaInfoMapper.updatePropagandaInfo(propagandaInfo);
		if (flag > 0) {
			// 删除之前的关联关系
			propagandaMaterialsMapper.deleteByPropagandaId(propagandaInfo.getId());
			// 修改宣教素材关联关系
			if (CollectionUtils.isNotEmpty(propagandaInfoSaveDto.getMaterialsInfoList())) {
				for (MaterialsInfo materialsInfo : propagandaInfoSaveDto.getMaterialsInfoList()) {
					PropagandaMaterials propagandaMaterials = new PropagandaMaterials();
					propagandaMaterials.setPropagandaId(propagandaInfo.getId());
					propagandaMaterials.setPropagandaTitle(propagandaInfo.getPropagandaTitle());
					propagandaMaterials.setMaterialsId(materialsInfo.getId());
					propagandaMaterials.setMaterialsName(materialsInfo.getMaterialsName());
					propagandaMaterials.setCreateTime(LocalDateTime.now());
					propagandaMaterials.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
					propagandaMaterials.setDelFlag(0);
					propagandaMaterialsMapper.insertPropagandaMaterials(propagandaMaterials);
				}
			}
		}
		return flag;
    }

    @Override
    public int copyPropagandaInfo(Long id) {
        PropagandaInfo propagandaInfo = propagandaInfoMapper.selectPropagandaInfoById(id);
        propagandaInfo.setId(null);
	    propagandaInfo.setDelFlag(0);
        propagandaInfo.setCreateTime(LocalDateTime.now());
        propagandaInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        propagandaInfo.setUpdateTime(null);
        propagandaInfo.setUpdateBy(null);
        propagandaInfo.setPropagandaCode(propagandaInfo.getPropagandaCode().replaceAll("-COPY", "") + "-COPY");
        propagandaInfo.setPropagandaStatus("CREATE_COMPLETE");
        int flag = propagandaInfoMapper.insertPropagandaInfo(propagandaInfo);
		if (flag > 0) {
			// 查询宣教素材关联关系
			PropagandaMaterials query = new PropagandaMaterials();
			query.setPropagandaId(id);
			List<PropagandaMaterials> propagandaMaterialsList = propagandaMaterialsMapper.selectPropagandaMaterialsList(query);
			if (CollectionUtils.isNotEmpty(propagandaMaterialsList)) {
				for (PropagandaMaterials propagandaMaterials : propagandaMaterialsList) {
					propagandaMaterials.setPropagandaId(id);
					propagandaMaterials.setPropagandaTitle(propagandaInfo.getPropagandaTitle());
					propagandaMaterials.setDelFlag(0);
					propagandaMaterials.setCreateBy(SecurityUtils.getLoginUser().getUser().getCreateBy());
					propagandaMaterials.setCreateTime(LocalDateTime.now());
					propagandaMaterialsMapper.insertPropagandaMaterials(propagandaMaterials);
				}
			}
		}
		return flag;
    }

    @Override
    public int updateStatus(PropagandaUpdateDto propagandaInfo) {
        PropagandaInfo body = new PropagandaInfo();
        body.setId(propagandaInfo.getId());
        body.setPropagandaStatus(propagandaInfo.getPropagandaStatus());
        return propagandaInfoMapper.updatePropagandaInfo(body);
    }

    /**
     * 批量删除宣教库管理
     *
     * @param ids 需要删除的宣教库管理主键
     * @return 结果
     */
    @Override
    public int deletePropagandaInfoByIds(Long[] ids) {
		// 删除宣教素材关系
	    propagandaMaterialsMapper.deleteByPropagandaIds(ids);
        return propagandaInfoMapper.deletePropagandaInfoByIds(ids);
    }

    /**
     * 删除宣教库管理信息
     *
     * @param id 宣教库管理主键
     * @return 结果
     */
    @Override
    public int deletePropagandaInfoById(Long id) {
        return propagandaInfoMapper.deletePropagandaInfoById(id);
    }

    @Override
    @DataScope(agencyAlias = "d", deptAlias = "p")
    public DepartmentBaseVo selectNumByDept(PropagandaInfoDto propagandaInfoDto) {
	    Agency agency = agencyMapper.selectAgencyById(propagandaInfoDto.getHospitalAgencyId());
	    List<DepartmentVO> departmentVOList = propagandaInfoMapper.selectNumByDept(propagandaInfoDto);
	    return DepartmentBaseVo.builder().hospitalAgencyName(ObjectUtils.isEmpty(agency) ? "" : agency.getAgencyName())
		    .deptNumList(departmentVOList).build();
    }
}
