package com.hotent.project.manager.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.baseInfo.manager.AuditedUnitManager;
import com.hotent.baseInfo.resp.AuditedUnitDetailResp;
import com.hotent.constant.BaseConstant;
import com.hotent.project.model.CostNotice;
import com.hotent.project.dao.CostNoticeDao;
import com.hotent.project.manager.CostNoticeManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectMaterial;
import com.hotent.project.req.CostProjectNoticePageReq;
import com.hotent.uc.model.User;
import com.hotent.uc.util.ContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;


/**
 * 通知公告表 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-27
 */
@Service
public class CostNoticeManagerImpl extends BaseManagerImpl<CostNoticeDao, CostNotice> implements CostNoticeManager {

	@Autowired
	private CostProjectApprovalManagerImpl projectApprovalManager;

	@Autowired
	private AuditedUnitManager auditedUnitManager;

	@Override
	public CostNotice getDetail(String id) {
    	CostNotice costNotice = this.get(id);
		if(costNotice == null){
			return costNotice;
		}
		if(costNotice.getProjectId() != null){
			CostProjectApproval costProjectApproval = projectApprovalManager.getById(costNotice.getProjectId());
			if(costProjectApproval != null){
				costNotice.setProjectName(costProjectApproval.getProjectName());
			}
		}
		String enterpriseId = costNotice.getEnterpriseId();
		if(StringUtils.isNotBlank(enterpriseId)){
			String[] split = enterpriseId.split(",");
			StringBuilder sb = new StringBuilder();
			for (String s : split) {
				AuditedUnitDetailResp detail = auditedUnitManager.detail(s);
				if(detail != null){
					sb.append(detail.getUnitName()).append(",");
				}
			}
			costNotice.setEnterpriseName(sb.toString().substring(0,sb.length()-1));
		}

		return costNotice;
	}
	@Override
	@Transactional
	public void createOrUpdate(CostNotice costNotice) {
		//新建或更新
		this.saveOrUpdate(costNotice);
	}

	@Override
	public IPage<CostNotice> pageList(CostProjectNoticePageReq req) {
		LambdaQueryWrapper<CostNotice> qw = new LambdaQueryWrapper<>();

		if(ObjectUtil.isNotEmpty(req.getTitle())){
			qw.like(CostNotice::getNoticeTitle, req.getTitle());
		}
		if(ObjectUtil.isNotEmpty(req.getContent())){
			qw.like(CostNotice::getNoticeContent, req.getContent());
		}
		if(ObjectUtil.isNotEmpty(req.getProjectId())){
			qw.eq(CostNotice::getProjectId, req.getProjectId());
		}
		if(ObjectUtil.isNotEmpty(req.getTaskId())){
			qw.eq(CostNotice::getTaskId, req.getTaskId());
		}
//		if(ObjectUtil.isNotEmpty(req.getEnterpriseId())){
//			qw.eq(CostNotice::getEnterpriseId, req.getEnterpriseId());
//		}
		qw.orderByDesc(CostNotice::getCreateTime);
		IPage<CostNotice> page=new Page<>(req.getPageNum(), req.getPageSize());
		return this.page(page, qw);
	}

	@Override
	public void sendNotice(String projectId,String taskId, String number, String title, String content, String enterpriseId, String noticeSource, String sendTarget) {
		CostNotice costNotice = new CostNotice();
		costNotice.setNoticeTitle(title);
		costNotice.setNoticeType(number);
		costNotice.setProjectId(projectId);
		costNotice.setTaskId(taskId);
		costNotice.setStatus(BaseConstant.NORMAL_STATUS);
		costNotice.setNoticeContent(content);
		costNotice.setEnterpriseId(enterpriseId);
		costNotice.setNoticeSource(noticeSource);
		costNotice.setSendTarget(sendTarget);
		this.save(costNotice);
	}

	@Override
	public void sendNotice(String projectId ,String noticeType, String noticeTitle, String noticeContent) {
		CostNotice costNotice = new CostNotice();
		costNotice.setNoticeTitle(noticeTitle);
		costNotice.setNoticeType("1");
		costNotice.setProjectId(projectId);
		costNotice.setStatus(BaseConstant.NORMAL_STATUS);
		costNotice.setNoticeContent(noticeContent);
		this.save(costNotice);
	}

	@Override
	public String read(String id) {
		CostNotice costNotice = this.getById(id);
		User currentUser = ContextUtil.getCurrentUser();
		if(!currentUser.getId().equals(costNotice.getSendTarget())){
			return "";
		}
		costNotice.setStatus("1");
		this.update(costNotice);
		return "阅读成功";
	}
}
