package cn.iocoder.yudao.module.system.service.biz;

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/13  9:04
 * @Description:
 * @Version: 4.0
 */

import cn.hutool.core.util.ArrayUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.system.api.bpm.vo.ElderlyRetreatRecordPageReqApiVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyRetreatRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyRetreatRecordSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.RetreatPrintRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyRetreatRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyRetreatRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
//import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 长者退住申请记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class RetreatRecordServiceImpl  implements RetreatRecordService {


    @Resource
    private ElderlyRetreatRecordMapper retreatRecordMapper;

    @Resource
    @Lazy
    private BpmProcessInstanceApi bpmProcessInstanceApi;

    @Resource
    private AdminUserMapper adminUserMapper;

    @Autowired
    private BuildService buildService;

    @Override
    public Long createRetreatRecord(ElderlyRetreatRecordSaveReqVO createReqVO) {
        // 插入
        ElderlyRetreatRecordDO retreatRecord = BeanUtils.toBean(createReqVO, ElderlyRetreatRecordDO.class);
        retreatRecordMapper.insert(retreatRecord);
        // 返回
        return retreatRecord.getId();
    }

    @Override
    public void updateRetreatRecord(ElderlyRetreatRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRetreatRecordExists(updateReqVO.getId());
        // 更新
        ElderlyRetreatRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyRetreatRecordDO.class);
        retreatRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRetreatRecord(Long id) {
        // 校验存在
        validateRetreatRecordExists(id);
        // 删除
        retreatRecordMapper.deleteById(id);
    }

    private void validateRetreatRecordExists(Long id) {
        if (retreatRecordMapper.selectById(id) == null) {
//            throw exception(RETREAT_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ElderlyRetreatRecordDO getRetreatRecord(Long id) {
        return retreatRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ElderlyRetreatRecordRespVO> getRetreatRecordPage(ElderlyRetreatRecordPageReqApiVO pageReqVO) {
        return null;
    }

    @Override
    @TenantIgnore
    public RetreatPrintRespVO getRetreatRecordDetailById(Long id) {
        return retreatRecordMapper.selectPrintById(id);
    }

    @Override
    @TenantIgnore
    public RetreatPrintRespVO getRetreatRecordDetail(Long id, int queryType) {
        RetreatPrintRespVO printRespVO =  getRetreatRecordDetailById(id);
        // 打印数据
        if (queryType == 1) {
            String processInstanceId = bpmProcessInstanceApi.getProcessInstanceId(String.valueOf(id));
            List<String> approverList = bpmProcessInstanceApi.getApproverList(processInstanceId);
            String creator = printRespVO.getCreator();
            AdminUserDO adminUserDO = adminUserMapper.selectById(creator);
            QueryWrapper<AdminUserDO> wrapper = new QueryWrapper<>();
            List<AdminUserDO> adminUserDOS = new ArrayList<>();
            if (approverList != null){
                wrapper.in("id", approverList);
                adminUserDOS = adminUserMapper.selectList(wrapper);
            }
            if (CollectionUtils.isNotEmpty(adminUserDOS)) {
                String nicknames = adminUserDOS.stream()
                        .filter(Objects::nonNull)
                        .map(AdminUserDO::getNickname)
                        .filter(Objects::nonNull)
                        .collect(Collectors.joining(","));
                printRespVO.setApprovalNickname(nicknames);
            }
            printRespVO.setCreateNickname(adminUserDO.getNickname());
            printRespVO.setApprovalOpinion("审批通过"); // 后续删除
        }
        printRespVO.setBedName(buildService.getFullBedName(printRespVO.getElderlyId()));
        return printRespVO;
    }
}
