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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.rlzysc.blo.JyzxglBLO;
import com.zjrs.zwnw.rlzysc.dto.*;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyJyzxFjxxMapper;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyJyzxMapper;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyJyzxFjxxPO;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyJyzxPO;
import com.zjrs.zwnw.rlzysc.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JyzxglBLOimpl extends BLOImpl implements JyzxglBLO {

    private static final Logger logger = LoggerFactory.getLogger(JyzxglBLOimpl.class);

    @Resource
    private VRsRlzyJyzxMapper vRsRlzyJyzxMapper;

    @Resource
    private VRsRlzyJyzxFjxxMapper vRsRlzyJyzxFjxxMapper;

    @Override
    public PageResult getJyzxglList(JyzxglQueryDTO dto) {
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        String userregioncode = UserUtil.getBae001(currentUser);//行政区划代码
        dto.setUserregioncode(userregioncode);
        logger.info("当前用户所在区划=="+userregioncode);
        Page<Map<String, Object>> pageResult = (Page) vRsRlzyJyzxMapper.getJyzxglList(page, dto);
        List<Map<String, Object>> list = pageResult.getRecords();
        
        pageResult.setRecords(list);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public String updateJyzxFbzt(ZjUDTO dto) {
        VRsRlzyJyzxPO po = new VRsRlzyJyzxPO();
        po.setFbzt(dto.getFlag());
        po.setRsrlzyjyzxlsh(dto.getZjlsh().trim());
        int cnt = vRsRlzyJyzxMapper.updateById(po);
        return cnt<=0?"0":po.getRsrlzyjyzxlsh();
    }

    @Override
    public String delJyzxXx(ZjDTO dto) {
        VRsRlzyJyzxPO po = new VRsRlzyJyzxPO();
        po.setRsrlzyjyzxlsh(dto.getZjlsh().trim());
        int cnt = vRsRlzyJyzxMapper.updateById(po);
        return cnt<=0?"0":po.getRsrlzyjyzxlsh();
    }

    @Override
    public Map getJyzxXx(ZjQueryDTO dto) throws Exception {
        Map<String,Object> res = new HashMap<String,Object>();
        if (StringUtils.isNotBlank(dto.getFlag())) {
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dataString = dateFormat.format(date);
            CurrentUser currentUser = getCurrentUser();
            String username = currentUser.getUsername() == null ?"":currentUser.getUsername().trim();
            String orgname = currentUser.getOrgname() == null ?"":currentUser.getOrgname().trim();
            String flag = dto.getFlag().trim();
            if ("add".equals(flag)){
                res.put("bae002",username);//创建人
                res.put("bae003",dataString);//创建时间
                res.put("bae006",orgname);//修改系统机构代码
            } 
        } else {
            throw new AppException("flag参数不能为空");
        }
        return res;
    }

    public  String ClobToString(Clob clob) throws SQLException, IOException {
        String reString = "";
        StringBuffer sb = new StringBuffer();
        reString = sb.toString();
        return reString;
    }

    @Override
    public String saveJyzxXx(JyzxglUDTO dto) {
        VRsRlzyJyzxPO po = new VRsRlzyJyzxPO();
        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() == null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid() == null?"":currentUser.getOrgid().trim();
        po.setBae004(userid);//最近修改人
        po.setBae005(dataString);//最近修改时间
        po.setBae006(orgid);//修改系统机构代码
        if (StringUtils.isNotBlank(po.getRsrlzyjyzxlsh())){
            //修改
            vRsRlzyJyzxMapper.updateById(po);
        } 
        return po.getRsrlzyjyzxlsh();
    }

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

    @Override
    public String saveFjxx(RsRlzyJyzxFjxxDTO dto) {
        VRsRlzyJyzxFjxxPO po = new VRsRlzyJyzxFjxxPO();
        BeanUtils.copyProperties(dto, po);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();

        String orgid = currentUser.getOrgid() == null?"":currentUser.getOrgid().trim();

        po.setBae005(dataString);//最近修改时间
        po.setBae006(orgid);//修改系统机构代码

        vRsRlzyJyzxFjxxMapper.insert(po);

        return po.getRsrlzyjyzxfjxxlsh();
    }

    @Override
    public void deleteFjxx(ZjDTO dto) {
        vRsRlzyJyzxFjxxMapper.deleteById(dto.getZjlsh());
    }
}
