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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.portal.blo.InformationBLO;
import com.zjrs.zwnw.portal.dto.ChangeInformationDTO;
import com.zjrs.zwnw.portal.dto.DelInformationDTO;
import com.zjrs.zwnw.portal.dto.GetInformationDTO;
import com.zjrs.zwnw.portal.dto.SendInformationDTO;
import com.zjrs.zwnw.portal.entity.VRsSyInformationPOMapper;
import com.zjrs.zwnw.portal.entity.VRsSyXxglPOMapper;
import com.zjrs.zwnw.portal.entity.VRsSyXxglSjPOMapper;
import com.zjrs.zwnw.portal.po.VRsSyInformationPO;
import com.zjrs.zwnw.portal.po.VRsSyXxglPO;
import com.zjrs.zwnw.portal.po.VRsSyXxglSjPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InformationBLOimpl extends BLOImpl implements InformationBLO {
    @Resource
    private VRsSyInformationPOMapper vRsSyInformationPOMapper;

    @Resource
    private VRsSyXxglSjPOMapper vRsSyXxglSjPOMapper;

    @Resource
    private VRsSyXxglPOMapper vRsSyXxglPOMapper;

    @Override
    public PageResult queryinformationList(GetInformationDTO dto, int page, int rows, String userid) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sjrid", userid);
        Page<VRsSyInformationPO> page1 = new Page<>(page, rows);
        // 查询条件
        QueryWrapper<VRsSyInformationPO> queryWrapper = new QueryWrapper<VRsSyInformationPO>();
        queryWrapper.allEq(params).orderByDesc("ts_ot");
        Page<VRsSyInformationPO> pageResult = (Page) this.vRsSyInformationPOMapper.selectPage(page1, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void changeInfo(ChangeInformationDTO dto) {
        String tsyxbz = dto.getTsyxbz();
        String[] id = dto.getIds().split(",");

        List<String> idsList = Arrays.asList(id);
        VRsSyXxglSjPO po = new VRsSyXxglSjPO();
        String ydzt = dto.getYdzt();

        vRsSyXxglSjPOMapper.update(po, new UpdateWrapper<VRsSyXxglSjPO>().in("rs_sy_xxgl_sj", idsList));
    }

    @Override
    public void delInfo(DelInformationDTO dto) {
        String[] id = dto.getIds().split(",");
        List<String> idsList = Arrays.asList(id);
        VRsSyXxglSjPO po = new VRsSyXxglSjPO();
        vRsSyXxglSjPOMapper.update(po, new UpdateWrapper<VRsSyXxglSjPO>().in("rs_sy_xxgl_sj", idsList));
    }

    @Override
    public void clearInfo(String userid) {
        VRsSyXxglSjPO po = new VRsSyXxglSjPO();
        po.setYdzt("");
        vRsSyXxglSjPOMapper.update(po, new UpdateWrapper<VRsSyXxglSjPO>().eq("sjrid", userid).eq("ts_yxbz", "0"));
    }

    @Override
    public void sendInfo(SendInformationDTO dto) {
        VRsSyXxglPO po = new VRsSyXxglPO();

        // 发件表插入数据
        BeanUtils.copyProperties(dto, po);
        vRsSyXxglPOMapper.insert(po);
        String fjid = po.getRssyxxgllsh();
        // 收件表插入数据
        List<String> sjrlist = dto.getSjrid();
        for (int i = 0; i < sjrlist.size(); i++) {
            String sjrid = sjrlist.get(i);
            VRsSyXxglSjPO PO = new VRsSyXxglSjPO();
            PO.setRssyxxgllsh(fjid);
            PO.setSjrid(sjrid);
            vRsSyXxglSjPOMapper.insert(PO);
        }
    }
}
