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.apache.commons.lang.StringUtils;
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 org.mohrss.leaf.core.framework.util.CurrentUser;
import com.zjrs.zwnw.portal.blo.CommonBLO;
import com.zjrs.zwnw.portal.blo.FbglBLO;
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 jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FbglBLOimpl extends BLOImpl implements FbglBLO {
    @Resource
    private VRsSyXxlxPOMapper vRsSyXxlxPOMapper;

    @Resource
    private VRsSyXxlxFjPOMapper vRsSyXxlxFjPOMapper;

    @Resource
    private VRsSyXxlxFjlistMapper vRsSyXxlxFjlistMapper;

    @Resource
    private VRsSyXxlxlistMapper vRsSyXxlxlistMapper;

    @Resource
    private VRrsSyXxlxzwMapper vRrsSyXxlxzwMapper;

    @Resource
    private VSSgOrgMapper vSSgOrgMapper;

    @Resource
    private VRsWbYjfkMapper vRsWbYjfkMapper;

    @Resource
    private VDwgrYjfkMapper vDwgrYjfkMapper;

    @Resource
    private VRsSyZskMapper vRsSyZskMapper;

    @Resource
    private CommonBLO commonBLO;

    @Override
    public String saveFbglZw(SaveFbglZwDTO dto) {
        VRsSyXxlxPO po = new VRsSyXxlxPO();
        BeanUtils.copyProperties(dto, po);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        String orgid = currentUser.getOrgid();
        po.setBae006(orgid);//修改系统机构代码
        po.setBae003(dataString);//创建时间
        vRsSyXxlxPOMapper.insert(po);
        return po.getRsxxlxlsh();
    }

    @Override
    public void saveFbglFj(SaveFbglFjDTO dto) {
        VRsSyXxlxFjPO po = new VRsSyXxlxFjPO();
        // BeanUtils.copyProperties(dto, po);
        po.setRsxxlxlsh(dto.getId());
        po.setFjtyccid(dto.getFjtyccid());
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        po.setBae004(userid);//最近修改人
        po.setBae003(dataString);//创建时间
        vRsSyXxlxFjPOMapper.insert(po);
    }

    @Override
    public void delFbglFj(CommonFbglDTO dto) {
        VRsSyXxlxFjPO po = new VRsSyXxlxFjPO();
        po.setTsyxbz("0");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        String orgid = currentUser.getOrgid();
        po.setBae004(userid);//最近修改人
        po.setBae006(orgid);//修改系统机构代码
        vRsSyXxlxFjPOMapper.update(po, new UpdateWrapper<VRsSyXxlxFjPO>().eq("rs_xxlx_fj", dto.getId()));
    }

    @Override
    public PageResult getFbglFjList(GetFbglFjListDTO dto) {
        Page<VRsSyXxlxFjlistPO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        // 查询条件
        QueryWrapper<VRsSyXxlxFjlistPO> queryWrapper = new QueryWrapper<VRsSyXxlxFjlistPO>();
        Page<VRsSyXxlxFjlistPO> pageResult = (Page) this.vRsSyXxlxFjlistMapper.selectPage(page1, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public VRrsSyXxlxzwPO getFbglZw(CommonFbglDTO dto) {
        VRrsSyXxlxzwPO po = new VRrsSyXxlxzwPO();
        VRrsSyXxlxzwPO po1 = vRrsSyXxlxzwMapper.selectOne(new QueryWrapper<VRrsSyXxlxzwPO>()
                .eq("rs_xxlx_lsh", dto.getId()));
        return po1;
    }

    @Override
    public PageResult getFbglList(GetFbglListDTO dto) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ts_yxbz", "1");
        Page<VRsSyXxlxlistPO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        // 查询条件
        QueryWrapper<VRsSyXxlxlistPO> queryWrapper = new QueryWrapper<VRsSyXxlxlistPO>();
        if (StringUtils.isNotBlank(dto.getBt())) {
            queryWrapper.like("bt", dto.getBt().trim());
        }
        if (StringUtils.isNotBlank(dto.getFbjg())) {
            // 获取其子组织机构
            QueryWrapper<VSSgOrgPO> queryWrapper1 = new QueryWrapper<VSSgOrgPO>();
            queryWrapper1.select("id").eq("is_valid", "1")
                    .eq("parent_id", dto.getFbjg().trim());
        }
        Page<VRsSyXxlxlistPO> pageResult = (Page) this.vRsSyXxlxlistMapper.selectPage(page1, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public PageResult getfbglNr(GetFbglListDTO dto) {
        VRsSyXxlxlistPO po = new VRsSyXxlxlistPO();
        Map<String, Object> params = new HashMap<String, Object>();
        Page<VRsSyXxlxlistPO> page1 = new Page<>(dto.getPagenum(), dto.getPagesize());
        QueryWrapper<VRsSyXxlxlistPO> queryWrapper = new QueryWrapper<VRsSyXxlxlistPO>();
        queryWrapper.like("fbqd", "1");
        queryWrapper.orderByDesc("zdbz").orderByDesc("zdrq").orderByDesc("fbrq");
        Page<VRsSyXxlxlistPO> pageResult = (Page) this.vRsSyXxlxlistMapper.selectPage(page1, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateFbglFbzt(ChangeFbglDTO dto) {
        VRsSyXxlxPO po = new VRsSyXxlxPO();
        po.setFbzt(dto.getFlag());
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        po.setFbr(userid);// 发布人
        vRsSyXxlxPOMapper.update(po, new UpdateWrapper<VRsSyXxlxPO>().eq("rs_xxlx", dto.getId()));
    }

    @Override
    public void updateFbglZdzt(ChangeFbglDTO dto) {
        VRsSyXxlxPO po = new VRsSyXxlxPO();
        po.setZdbz(dto.getFlag());
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        po.setZdsj(dataString);// 置顶时间
        vRsSyXxlxPOMapper.update(po, new UpdateWrapper<VRsSyXxlxPO>().eq("rs_xxlx", dto.getId()));
    }

    @Override
    public void delFbgl(CommonFbglDTO dto) {
        VRsSyXxlxPO po = new VRsSyXxlxPO();
        po.setTsyxbz("0");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid();
        vRsSyXxlxPOMapper.update(po, new UpdateWrapper<VRsSyXxlxPO>().eq("rs_xxlx", dto.getId()));
    }

    @Override
    public PageResult getYjfkList(GetYjfkListDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) vDwgrYjfkMapper.getYjfkList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public Map getYjfk(String id) {
        return vDwgrYjfkMapper.getYjfk(id);
    }

    @Override
    public String saveYjfk(SaveYjfkDTO dto) {
        VRsWbYjfk po = new VRsWbYjfk();
        po.setClyj(dto.getClyj());
        //获取回复时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date) == null ? "" : dateFormat.format(date).trim();
        po.setHfsj(dataString);
        //获取回复人
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        po.setHfr(userid);
        vRsWbYjfkMapper.update(po,new UpdateWrapper<VRsWbYjfk>().eq("rs_wb_yjfk",dto.getRswbyjfklsh()));
        return dto.getRswbyjfklsh();
    }

    @Override
    public PageResult getZskList(ZskQueryDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        if(dto.getBae001Flag().equals("02")) {
            dto.setBae001(orgid);
        }
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> vRsSyZskPOIPage = vRsSyZskMapper.getZskList(page, dto);
        PageResult result = PageUtil.PageResult(vRsSyZskPOIPage);
        return result;
    }

    @Override
    public VRsSyZskPO saveZsk(ZskDTO dto) {
        VRsSyZskPO zskPO = new VRsSyZskPO();
        BeanUtils.copyProperties(dto,zskPO,"bae001","bae002","bae003");
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = commonBLO.getCurrenttime();
        zskPO.setBae004(userid);//最近修改人
        zskPO.setBae005(currentTime);//最近修改时间
        zskPO.setBae006(orgid);//最近修改人机构
        ZskDTO zskDTO = new ZskDTO();
        zskDTO.setRssyzsklsh(zskPO.getRssyzsklsh());
        VRsSyZskPO zskXx = getZskXxById(zskDTO); //保存后再查询回显
        return zskXx;
    }

    @Override
    public void deleteZsk(ZskDTO dto) {
        VRsSyZskPO rsSyZskPO = new VRsSyZskPO();
        rsSyZskPO.setTsyxbz("0");
        rsSyZskPO.setRssyzsklsh(dto.getRssyzsklsh());
        vRsSyZskMapper.updateById(rsSyZskPO);
    }

    @Override
    public void publishZsk(ZskDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        VRsSyZskPO zskPO = new VRsSyZskPO();
        zskPO.setFbzt(dto.getFbzt());
        UpdateWrapper<VRsSyZskPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("RS_SY_ZSK",dto.getRssyzsklsh());
        vRsSyZskMapper.update(zskPO,updateWrapper);
    }

    @Override
    public VRsSyZskPO getZskXxById(ZskDTO dto) {
        return vRsSyZskMapper.getZskXxById(dto.getRssyzsklsh());
    }

    @Override
    public void exportZsk(ZskQueryDTO dto, HttpServletResponse res) {
        InputStream inputStream = null;
        try {
            inputStream = FbglBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "portal" + File.separator + "zskExport.xlsx");

            String fileName = "知识库列表.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getZskFileList(ZskQueryDTO dto) {
        //根据条件查询出id集合
        Map<String,String> resultMap = vRsSyZskMapper.getZskIds(dto);
        String ids = resultMap.get("ids");
        //根据ID查出所有附件
        List<String> list = Arrays.asList(ids.split(","));
       return vRsSyZskMapper.getZskFileList(dto,list);
    }

}
