package com.zjrs.zwnw.bsdb.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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.bsdb.blo.DbgzBLO;
import com.zjrs.zwnw.bsdb.dto.*;
import com.zjrs.zwnw.bsdb.entity.DbgzMapper;
import com.zjrs.zwnw.bsdb.entity.DbsxMapper;
import com.zjrs.zwnw.bsdb.po.RsBsdbDbgzPo;
import com.zjrs.zwnw.bsdb.po.RsBsdbDbsxPo;
import com.zjrs.zwnw.bsdb.util.AppUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

@Service
public class DbgzBLOImpl extends BLOImpl implements DbgzBLO {

    @Resource
    DbgzMapper dbgzMapper;

    @Resource
    DbsxMapper dbsxMapper;

    @Override
    public PageResult queryDbglRuleSelectList(DbglPageQueryDTO dto) {
        Page<DbglDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        dto.setDbr(currentUser.getUserid());
        Page<DbglDTO> pageResult = (Page) dbgzMapper.queryDbglRuleSelectList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public PageResult queryDbglRuleList(DbglPageQueryDTO dto) {
        Page<DbglDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        dto.setDbr(currentUser.getUserid());
        System.err.println(currentUser.getUserid());
        Page<DbglDTO> pageResult = (Page) dbgzMapper.queryDbglRuleList(page, dto);
        return PageUtil.PageResult(pageResult);
    }


    @Override
    public PageResult queryDbglTrackList(DbglPageQueryDTO dto) {
        Page<DbglDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        dto.setDbr(currentUser.getUserid());
        Page<DbglDTO> pageResult = (Page) dbgzMapper.queryDbglDbrwList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public PageResult queryDbglFeedbackList(DbglPageQueryDTO dto) {
        Page<DbglDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        dto.setFzr(currentUser.getUserid());
        dto.setRsbsdbpfzt("1");
        Page<DbglDTO> pageResult = (Page) dbgzMapper.queryDbglDbrwList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public PageResult queryDbglTaskList(DbglPageQueryDTO dto) {
        Page<DbglDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        dto.setFzr(currentUser.getUserid());
        Page<DbglDTO> pageResult = (Page) dbgzMapper.queryDbglTaskList(page, dto);
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public DbglDTO dbglRuleSave(DbglRuleSaveDTO dto) {
        RsBsdbDbgzPo rsBsdbDbgzPo = new RsBsdbDbgzPo();
        BeanUtils.copyProperties(dto, rsBsdbDbgzPo);

        if (StringUtils.isBlank(dto.getRsbsdbdbgzlsh())) {
            AppUtils.initParams(rsBsdbDbgzPo);
            dbgzMapper.insert(rsBsdbDbgzPo);
        } else {
            RsBsdbDbgzPo po = this.getRtnRsBsdbDbgz(dto.getRsbsdbdbgzlsh());
            QueryWrapper<RsBsdbDbsxPo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("rs_bsdb_dbsx", po.getRsbsdbdbsxlsh());
            RsBsdbDbsxPo dbsxPo = dbsxMapper.selectOne(queryWrapper);
            String rsbsdbpfzt = dbsxPo.getRsbsdbpfzt();
            if (StringUtils.equals("1", rsbsdbpfzt)) {
                throw new AppException("当前督办事项已派发, 无法修改, 请核查");
            }

            AppUtils.updateParams(rsBsdbDbgzPo);
            dbgzMapper.updateById(rsBsdbDbgzPo);
        }
        return null;
    }

    @Override
    public void dbglRuleDelete(DbglRuleDeleteDTO dto) {
        RsBsdbDbgzPo po = this.getRtnRsBsdbDbgz(dto.getRsbsdbdbgzlsh());
        QueryWrapper<RsBsdbDbsxPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_bsdb_dbsx", po.getRsbsdbdbsxlsh());
        RsBsdbDbsxPo dbsxPo = dbsxMapper.selectOne(queryWrapper);
        String rsbsdbpfzt = dbsxPo.getRsbsdbpfzt();

        RsBsdbDbgzPo rsBsdbDbgzPo = new RsBsdbDbgzPo();
        rsBsdbDbgzPo.setRsbsdbdbgzlsh(dto.getRsbsdbdbgzlsh());
        dbgzMapper.updateById(rsBsdbDbgzPo);
    }

    @Override
    public DbglDTO queryDbglRule(DbglRuleQueryDTO dto) {
        return dbgzMapper.queryDbglRule(dto);
    }


    /**
     * 根据流水号获取督办事项信息
     * @param rsbsdbdbgzlsh
     * @return
     */
    private RsBsdbDbgzPo getRtnRsBsdbDbgz(String rsbsdbdbgzlsh) {
        QueryWrapper<RsBsdbDbgzPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_bsdb_dbgz", rsbsdbdbgzlsh);

        return dbgzMapper.selectOne(queryWrapper);
    }
}
