package com.hotent.project.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.exception.BaseException;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.project.dao.CostProjectSuperviseDao;
import com.hotent.project.dao.CostProjectSurveyDao;
import com.hotent.project.manager.CostProjectApprovalManager;
import com.hotent.project.manager.CostProjectSuperviseDataManager;
import com.hotent.project.manager.CostProjectSuperviseManager;
import com.hotent.project.manager.CostProjectSurveyManager;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectSupervise;
import com.hotent.project.model.CostProjectSuperviseData;
import com.hotent.project.model.CostProjectSurvey;
import com.hotent.project.req.CostProjectSuperviseReq;
import com.hotent.project.vo.CostProjectSuperviseDataVo;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.User;
import com.hotent.uc.util.ContextUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 *@author: zhao yue yue
 *@create: 2025-11-18 14:48
 */
@Service
public class CostProjectSuperviseManagerImpl extends BaseManagerImpl<CostProjectSuperviseDao, CostProjectSupervise> implements CostProjectSuperviseManager {

    @Autowired
    private CostProjectApprovalManager costProjectApprovalManager;

    @Autowired
    private UserManager userService;

    @Autowired
    private CostProjectSuperviseDataManager costProjectSuperviseDataManager;

    @Override
    public String add(CostProjectSuperviseReq param) throws Exception {
        if (StringUtil.isEmpty(param.getProjectId())){
            throw new BaseException("项目id不能够为空");
        }
        if (StringUtil.isEmpty(param.getSupervisorId())) {
            throw new BaseException("督办人不能为空");
        }
        User supervisor = userService.get(param.getSupervisorId());
        if (supervisor==null) {
            throw new BaseException("根据id【"+param.getSupervisorId()+"】督办人信息未查询到");
        }
        CostProjectApproval projectApproval = costProjectApprovalManager.get(param.getProjectId());
        if (projectApproval==null) {
            throw new BaseException("根据id：【"+param.getProjectId()+"】未查询到项目信息");
        }
        param.setSupervisorId(supervisor.getId());
        param.setSupervisorName(supervisor.getFullname());
        param.setCreateTime(LocalDateTime.now());
        param.setId(String.valueOf(UniqueIdUtil.getUId()));
        param.setReportTime(LocalDateTime.now());
        param.setStatus(0);
        String account = ContextUtil.getCurrentUser().getAccount();
        User user = userService.getByAccount(account);
        param.setCreateId(user.getId());
        param.setCreateBy(user.getFullname());
        boolean save = this.save(param);
        return save ? "添加成功" :"添加失败";
    }

    @Override
    public String modify(CostProjectSuperviseReq param) throws Exception {
        if(StringUtil.isEmpty(param.getId())){
            throw new BaseException("实体id不能为空");
        }
        CostProjectSupervise supervise = this.get(param.getId());
        if (supervise==null) {
            throw new BaseException("未查询到实体信息");
        }
        String account = ContextUtil.getCurrentUser().getAccount();
        User user = userService.getByAccount(account);
        CostProjectSuperviseData costProjectSuperviseData = new CostProjectSuperviseData();
        costProjectSuperviseData.setId(String.valueOf(UniqueIdUtil.getUId()));
        costProjectSuperviseData.setReportContent(param.getReportContent());
        costProjectSuperviseData.setReportTime(LocalDateTime.now());
        costProjectSuperviseData.setSuperviseId(param.getId());
        costProjectSuperviseData.setProjectId(supervise.getProjectId());
        costProjectSuperviseData.setCreateBy(user.getFullname());
        costProjectSuperviseData.setCreateTime(LocalDateTime.now());
        costProjectSuperviseData.setCreateId(user.getId());
        costProjectSuperviseData.setAttachmentIds(param.getAttachmentUrls());
        //boolean b = this.saveOrUpdate(param);
        //this.update(param);
        boolean save = costProjectSuperviseDataManager.save(costProjectSuperviseData);
        return save ? "上报成功" :"上报失败";
    }

    @Override
    public String delete(CostProjectSuperviseReq param) {
        if(StringUtil.isEmpty(param.getId())){
            throw new BaseException("实体id不能为空");
        }
        CostProjectSupervise supervise = this.get(param.getId());
        if (supervise==null) {
            throw new BaseException("未查询到实体信息");
        }
        supervise.setStatus(1);
        this.update(supervise);
        return "办结成功";
    }

    @Override
    public List<CostProjectSupervise> getListByprojectIds(List<String> projectIds) {
        if (projectIds!=null && !projectIds.isEmpty()) {
            QueryWrapper<CostProjectSupervise> wrapper = new QueryWrapper<>();
            wrapper.in("project_id",projectIds);
            return this.list(wrapper);
        }
        return null;
    }

    @Override
    public List<CostProjectSuperviseDataVo> getReportList(CostProjectSuperviseReq param) {
        if(StringUtil.isEmpty(param.getId())){
            throw new BaseException("ID不能为空");
        }
        CostProjectSupervise supervise = this.get(param.getId());
        if (supervise==null) {
            throw new BaseException("根据id未查询到督办实体");
        }
        List<CostProjectSuperviseData> data= costProjectSuperviseDataManager.getListBySupervise(param.getId());
        List<CostProjectSuperviseDataVo> dataVos = data.stream().map(d -> {
            CostProjectSuperviseDataVo costProjectSuperviseDataVo = new CostProjectSuperviseDataVo();
            BeanUtils.copyProperties(d, costProjectSuperviseDataVo);
            costProjectSuperviseDataVo.setSuperviseId(supervise.getSupervisorId());
            costProjectSuperviseDataVo.setSupervisorName(supervise.getSupervisorName());
            return costProjectSuperviseDataVo;
        }).collect(Collectors.toList());
        return dataVos;
    }
}
