package com.zjrs.zwnw.portal.blo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import com.zjrs.zwnw.portal.blo.InformationBLO;
import com.zjrs.zwnw.portal.blo.RcglBLO;
import com.zjrs.zwnw.portal.dto.*;
import com.zjrs.zwnw.portal.entity.*;
import com.zjrs.zwnw.portal.po.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RcglBLOimpl extends BLOImpl implements RcglBLO {
    @Resource
    private VRsSyRcglMapper vRsSyRcglMapper;

    @Resource
    private VSsSyRcglOrgMapper vSSgUserOrgMapper;

    @Resource
    private VRsSyRcglnrMapper vRsSyRcglnrMapper;

    @Resource
    private InformationBLO informationBLO;

    @Resource
    private VRrsSyRcglAreaMapper vRrsSyRcglAreaMapper;

    @Resource
    private VRsSyOrgAreaMapper vRsSyOrgAreaMapper;

    @Override
    public List<Object> getRcgltimeline(ChangeFbglDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        // 获取操作人的组织机构，行政区划
        VRsSyOrgAreaPO orgareapo = vRsSyOrgAreaMapper.selectOne(new QueryWrapper<VRsSyOrgAreaPO>()
                .select("org_id", "area_id")
                .eq("user_id", userid));
        String orgid = orgareapo.getOrgid();
        String areaid = orgareapo.getAreaid();
        List<Object> result = new ArrayList<Object>();
        if (dto.getFlag().equals("month")) {
            // 个人日程
            if (StringUtils.isNotBlank(userid)) {
                List<VRsSyRcglPO> result1 = vRsSyRcglMapper.selectList(new QueryWrapper<VRsSyRcglPO>()
                        .apply("to_date (rcsj,'YY-MM-DD HH24:MI:SS') <= last_day(to_date('" + (dto.getId() + " 23:59:59") + "','YY-MM-DD HH24:MI:SS'))")
                );
                result.addAll(result1);
            }
            // 同科室日程timeline
            if (StringUtils.isNotBlank(orgid)) {
                List<VSsSyRcglOrgPO> result2 = vSSgUserOrgMapper.selectList(new QueryWrapper<VSsSyRcglOrgPO>()
                        .eq("ts_yxbz", "1"));
                result.addAll(result2);
            }
            // 全局日程
            if (StringUtils.isNotBlank(areaid)) {
                List<VRrsSyRcglAreaPO> result3 = vRrsSyRcglAreaMapper.selectList(new QueryWrapper<VRrsSyRcglAreaPO>()
                        .eq("ts_yxbz", "1"));
                result.addAll(result3);
            }
        } else if (dto.getFlag().equals("day")) {
            // 个人日程管理和全局日程管理
            List<VRsSyRcglPO> result1 = vRsSyRcglMapper.selectList(new QueryWrapper<VRsSyRcglPO>()
                    .or(i -> i.eq("userid", userid).eq("rcfl", "3")));
            // 同科室日程管理
            List<VSsSyRcglOrgPO> result2 = vSSgUserOrgMapper.selectList(new QueryWrapper<VSsSyRcglOrgPO>()
                    .eq("ts_yxbz", "1"));
            result.addAll(result1);
            result.addAll(result2);
        }
        return result;
    }

    @Override
    public void saveRcgl(SaveRcglDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        String orgname = currentUser.getOrgname();
        String username = currentUser.getUsername();
        VRsSyRcglPO po = new VRsSyRcglPO();
        BeanUtils.copyProperties(dto, po);
        SendInformationDTO infodto = new SendInformationDTO();
        String rcflname = "";
        if (StringUtils.isNotBlank(dto.getId())) {
            // update
            vRsSyRcglMapper.update(po, new UpdateWrapper<VRsSyRcglPO>()
                    .eq("userid", userid));
            //infodto.setSjrid("");
            infodto.setXxnr(orgname + "的" + username + "修改了" + rcflname + "日程【" + po.getRcbt() + "】，请注意查看。");
        } 
        String rcfl = po.getRcfl();
        List<String> sjrList = this.getSjrList(userid, rcfl);
        infodto.setSjrid(sjrList);
        informationBLO.sendInfo(infodto);
    }

    @Override
    public void delRcgl(CommonFbglDTO dto) {
        VRsSyRcglPO po = new VRsSyRcglPO();
        po.setTsyxbz("0");
        vRsSyRcglMapper.update(po, new UpdateWrapper<VRsSyRcglPO>().eq("rs_sy_rcgl_lsh", dto.getId()));
        VRsSyRcglnrPO selectpo = vRsSyRcglnrMapper.selectOne(new QueryWrapper<VRsSyRcglnrPO>()
                .select("rs_sy_rcgl_lsh", "rcfl", "rcbt", "rcnr", "rq", "sj")
                .eq("rs_sy_rcgl_lsh", dto.getId()));
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        String orgname = currentUser.getOrgname();
        String username = currentUser.getUsername();
        SendInformationDTO infodto = new SendInformationDTO();
        infodto.setXxbt("日程删除通知");
        infodto.setFjrid("");
        String rcflname = "";
        infodto.setXxnr(orgname + "的" + username + "删除了" + rcflname + "日程【" + selectpo.getRcbt() + "】。");
        String rcfl = selectpo.getRcfl();
        List<String> sjrList = this.getSjrList(userid, rcfl);
        infodto.setSjrid(sjrList);
        informationBLO.sendInfo(infodto);
    }

    @Override
    public VRsSyRcglnrPO getRcgl(CommonFbglDTO dto) {
        VRsSyRcglnrPO po = vRsSyRcglnrMapper.selectOne(new QueryWrapper<VRsSyRcglnrPO>()
                .eq("rs_sy_rcgl_lsh", dto.getId()).eq("ts_yxbz", "1"));
        return po;
    }

    public List<String> getSjrList(String userid, String rcfl) {
        // 获取日程所需要接收的人，个人的，科室的，全局的
        List<String> sjrList = new ArrayList<String>();
        if (rcfl.equals("3")) {
            //个人日程
            // sjrList.add(userid);
        } else {
            // 查询日程操作人的组织机构和行政区划
            VRsSyOrgAreaPO orgareapo = vRsSyOrgAreaMapper.selectOne(new QueryWrapper<VRsSyOrgAreaPO>()
                     .eq("user_id", userid));
            String orgid = orgareapo.getOrgid();
            String areaid = orgareapo.getAreaid();
            if (rcfl.equals("2") && StringUtils.isNotBlank(orgid)) {
                //科室日程
                QueryWrapper<VRsSyOrgAreaPO> queryWrapper = new QueryWrapper<VRsSyOrgAreaPO>();
                queryWrapper.select("user_id").eq("org_id", orgid);
            } else if (rcfl.equals("1") && StringUtils.isNotBlank(areaid)) {
                //全局日程
                QueryWrapper<VRsSyOrgAreaPO> queryWrapper = new QueryWrapper<VRsSyOrgAreaPO>();
                queryWrapper.select("user_id").eq("area_id", areaid);
            }
        }
        return sjrList;
    }
}
