package com.zjrs.zwnw.rlzysc.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 com.zjrs.zwnw.rlzysc.blo.GxygBLO;
import com.zjrs.zwnw.rlzysc.dto.GxygDTO;
import com.zjrs.zwnw.rlzysc.dto.GxygQueryDTO;
import com.zjrs.zwnw.rlzysc.entity.GxygMapper;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyQyxxMapper;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyGxygPO;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyQyxxPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class GxygBLOimpl extends BLOImpl implements GxygBLO {

    @Autowired
    private GxygMapper gxygMapper;

    @Autowired
    private VRsRlzyQyxxMapper qyxxMapper;

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

    @Override
    public VRsRlzyGxygPO getGxyg(GxygDTO dto) {
        QueryWrapper<VRsRlzyGxygPO> queryWrapper = new QueryWrapper<>();

        VRsRlzyGxygPO gxygPO = this.gxygMapper.selectOne(queryWrapper);
        QueryWrapper<VRsRlzyQyxxPO> qyxxPOQueryWrapper = new QueryWrapper<>();
        VRsRlzyQyxxPO qyxxPo = this.qyxxMapper.selectOne(qyxxPOQueryWrapper);

        gxygPO.setQymc(qyxxPo.getGsmc());
        return gxygPO;
    }

    @Override
    public void updateState(GxygDTO dto) {
        UpdateWrapper<VRsRlzyGxygPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("zxzt", dto.getTsyxbz());
        this.gxygMapper.update(new VRsRlzyGxygPO(), updateWrapper);
    }

    @Override
    public void updateShztAndFbzt(GxygDTO dto) {
        UpdateWrapper<VRsRlzyGxygPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("shzt", dto.getShzt());
        updateWrapper.set(StringUtils.isNotBlank(dto.getShyj()), "shyj", dto.getShyj());

        this.gxygMapper.update(new VRsRlzyGxygPO(), updateWrapper);
    }

    @Override
    public Map<String, Object> getGxygStates() {
        return this.gxygMapper.getGxygStatus();
    }

    @Override
    public void updateGxygStates(GxygDTO dto) {
        this.gxygMapper.updateGxygStatus(dto.getCxzt());
    }
}
