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

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.dto.common.CommonDictDTO;
import com.zjrs.zwnw.rlzysc.blo.JxgwBLO;
import com.zjrs.zwnw.rlzysc.dto.JxgwCommDTO;
import com.zjrs.zwnw.rlzysc.dto.JxgwDTO;
import com.zjrs.zwnw.rlzysc.dto.JxgwQueryDTO;
import com.zjrs.zwnw.rlzysc.dto.JxgwSaveDTO;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyJxgwMapper;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyJxgwPO;
import com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class JxgwBLOimpl extends BLOImpl implements JxgwBLO {
    private static final Logger LOG = LoggerFactory.getLogger(JxgwBLOimpl.class);
    @Resource
    private VRsRlzyJxgwMapper vRsRlzyJxgwMapper;
    @Resource
    CommonBLO commBLO;
    @Override
    public PageResult getList(JxgwQueryDTO dto) {
        Page<JxgwDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<JxgwDTO> pageResult = (Page) vRsRlzyJxgwMapper.getList(page, dto);
        chargeSfyq(pageResult);

        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    @Override
    public VRsRlzyJxgwPO getDetail(JxgwCommDTO dto){
        return this.vRsRlzyJxgwMapper.getDetail(dto);
    }
    @Override
    public String saveJxgw(JxgwSaveDTO dto){
        VRsRlzyJxgwPO po = new VRsRlzyJxgwPO();
        BeanUtils.copyProperties(dto, po);
        po.setSfyq(dto.getSfyqs());
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String regioncode = currentUser.getRegioncode()==null?"":currentUser.getRegioncode().trim();

        String czsj = this.commBLO.getCurrenttime();//获取当前时间
        //截止日期不能小于当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date current = null;
        Date bmjzrq = null;

        po.setBae004(userid);//最近修改人
        if(StringUtils.isBlank(dto.getRsrlzyjxgwlsh())){//新增
            po.setBae001(regioncode);//创建系统机构代码,行政区划
            po.setBae002(userid);//创建人
            vRsRlzyJxgwMapper.insert(po);
        }else{//修改
            vRsRlzyJxgwMapper.updateById(po);
        }
        return po.getRsrlzyjxgwlsh();
    }
    @Override
    public void delJxgw(JxgwCommDTO dto){
        VRsRlzyJxgwPO po = new VRsRlzyJxgwPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        po.setBae004(userid);//最近修改人
        String ids = dto.getId();
        String[] idArr = ids.split(",");
        List<String> list = Arrays.asList(idArr);
        po.setTsyxbz("0");
    }
    @Override
    public void updateZt(JxgwCommDTO dto, String s){
        VRsRlzyJxgwPO po = new VRsRlzyJxgwPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        po.setBae004(userid);//最近修改人
    }
    @Override
    public void exportJxgw(JxgwQueryDTO dto, HttpServletResponse res){
        Page<JxgwDTO> page = new Page<>(1, 10000);
        //增加列表权限，只能查本级及下级机构数据
        CurrentUser currentUser = getCurrentUser();
        String regioncode = currentUser.getRegioncode()==null?"":currentUser.getRegioncode().trim();
        dto.setCurrentUserArea(regioncode);

        Page<JxgwDTO> pageResult = (Page) vRsRlzyJxgwMapper.getList(page, dto);
        chargeSfyq(pageResult);
        List<JxgwDTO> result = pageResult.getRecords();

        InputStream inputStream = null;
        try {
            inputStream = JcsxqdkBLOimpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "rlzy" + File.separator + "jxgwexport.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();
                }
            }
        }
    }
    //转换身份要求字典
    public void chargeSfyq(Page<JxgwDTO> pageResult){
        //取身份要求字典
        CommonDictDTO dictDTO = new CommonDictDTO();
        dictDTO.setDictArr(new String[]{"RS_RLZY_JXGW_SFYQ"});
        Map<String, Object> dictMap = this.commBLO.getDicts(dictDTO);
        Map<String, List> dictListMap = (Map<String, List>)dictMap.get("code_detail");
        List<Map<String, Object>> sfyqList = dictListMap.get("RS_RLZY_JXGW_SFYQ");
        //字典转换
        List<JxgwDTO> list = pageResult.getRecords();
        for(JxgwDTO jxgw: list){
            String sfyq = jxgw.getSfyq();
            if(StringUtils.isNotBlank(sfyq)){
                String sfyqValue = "";
                if(StringUtils.isNotBlank(sfyqValue)){
                    sfyqValue = sfyqValue.substring(0, sfyqValue.length() - 1);
                }
                jxgw.setSfyq(sfyqValue);
            }
        }
    }
}
