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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.dbgl.blo.DbrwglCommPLO;
import com.zjrs.zwnw.dbgl.dto.*;
import com.zjrs.zwnw.dbgl.entity.DbrwglCommMapper;
import com.zjrs.zwnw.dbgl.entity.DbxmGzqdMapper;
import com.zjrs.zwnw.dbgl.entity.DbxmZrbmMapper;
import com.zjrs.zwnw.dbgl.entity.PsxxMapper;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmgzqdPO;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmzrbmPO;
import com.zjrs.zwnw.dbgl.po.RsdbglpsxxPO;
import com.zjrs.zwnw.portal.blo.CommonBLO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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


/**
 * @Author chenzhibiao
 * @create 2022/6/30 8:43
 */
@Service
public class DbrwglCommimplPLO extends BLOImpl implements DbrwglCommPLO {
    @Resource
    private DbrwglCommMapper dbrwglCommMapper;
    @Resource
    private DbxmZrbmMapper dbxmZrbmMapper;
    @Resource
    private PsxxMapper psxxMapper;
    @Resource
    private CommonBLO commonBLO;
    @Resource
    private DbxmGzqdMapper dbxmGzqdMapper;



    @Override
    public List<DictDTO> getOrgs(DictGetDTO dto){
        if (StringUtils.isNotBlank(dto.getIsJgfzr())){
            CurrentUser currentUser = getCurrentUser();
            String userId = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
            dto.setUserid(userId);
            List<DictDTO>  resultList= this.dbrwglCommMapper.getJgfzrOrgs(dto);
            return resultList;
        }
        return this.dbrwglCommMapper.getOrgs(dto);
    }

    @Override
    public List<DictDTO> getUsers(DictGetDTO dto){
        return this.dbrwglCommMapper.getUsers(dto);
    }
    @Override
    public String getFgldByXmId(String xmid){
        DbrwglCommDTO dto = new DbrwglCommDTO();
        dto.setIds(xmid);
        Map<String,Object> qtbmFgld = this.dbrwglCommMapper.getQtbmFgldByXmid(dto);
        String fglds ="";

        if(StringUtils.isNotBlank(fglds)){
            fglds = fglds.substring(0,fglds.length() -1);
        }
        return fglds;
    }

    @Override
    public String getFgldByRwId(String rwid){
        DbrwglCommDTO dto = new DbrwglCommDTO();
        dto.setIds(rwid);
        List<Map<String,Object>> qtbmFgldList = this.dbrwglCommMapper.getQtbmFgldByRwid(dto);
        String fglds ="";
        return fglds;
    }

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

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

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

    @Override
    public void delPsjlById(DbrwglCommDTO dto){
        RsdbglpsxxPO po = new RsdbglpsxxPO();
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String currentTime = this.commonBLO.getCurrenttime();
        po.setBae004(userid);//最近修改人
        po.setBae005(currentTime);//最近修改时间
        this.psxxMapper.updateById(po);
    }
    @Override
    public void savePsjl(DbrwglPsjlUDTO dto){
        String id = dto.getRsdbglpsxxlsh();
        RsdbglpsxxPO po = new RsdbglpsxxPO();
        BeanUtils.copyProperties(dto, po);

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = this.commonBLO.getCurrenttime();
        po.setBae004(userid);//最近修改人

        if(StringUtils.isBlank(id)){//新增
            po.setBae001(orgid);
            this.psxxMapper.insert(po);
        }
    }
    @Override
    public RsdbgldbxmgzqdPO getGzqdjzqk(DbrwglCommDTO dto){
        QueryWrapper<RsdbgldbxmgzqdPO> queryWrapper = new QueryWrapper<RsdbgldbxmgzqdPO>();
        RsdbgldbxmgzqdPO maxshsj = this.dbxmGzqdMapper.selectOne(queryWrapper);

        QueryWrapper<RsdbgldbxmgzqdPO> queryWrapper2 = new QueryWrapper<RsdbgldbxmgzqdPO>();
        if(maxshsj != null){
            queryWrapper2.eq("shsj",maxshsj.getShsj());
        }
        return this.dbxmGzqdMapper.selectOne(queryWrapper2);
    }

    @Override
    public PageResult getDbtxList(DbrwglDbrwclGzjzshQueryDTO dto){
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        //userid ="1c872fa8-8681-42b4-a221-04f5a7759b74";
        //orgid = "564c3dea-b00d-47af-acd6-794baa41e688";
        dto.setUserid(userid);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = null;
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void savePjxx(DbrwglCommPjxxSaveDTO dto) {
        RsdbgldbxmzrbmPO po = new RsdbgldbxmzrbmPO();
        BeanUtils.copyProperties(dto,po);

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = this.commonBLO.getCurrenttime();
        po.setPjjg(orgid);//评价机构

        this.dbxmZrbmMapper.updateById(po);
    }

    @Override
    public DbrwglCommPjxxDTO getPjxx(String rsdbgldbxmzrbmlsh) {
        RsdbgldbxmzrbmPO po = this.dbxmZrbmMapper.selectById(rsdbgldbxmzrbmlsh);
        DbrwglCommPjxxDTO dto = new DbrwglCommPjxxDTO();
        BeanUtils.copyProperties(po,dto);
        return dto;
    }

}
