package com.zjrs.ggfw.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 org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.blo.MsgGlBLO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgUDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgglZtDTO;
import com.zjrs.ggfw.rlzysc.entity.QyxxMapper;
import com.zjrs.ggfw.rlzysc.entity.VRsRlzyMsgglMapper;
import com.zjrs.ggfw.rlzysc.po.RsRlzyQyxxPO;
import com.zjrs.ggfw.rlzysc.po.VRsRlzyMsgglPO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service
public class MsgGlBLOImpl extends BLOImpl implements MsgGlBLO {

    @Autowired
    private VRsRlzyMsgglMapper vRsRlzyMsgglMapper;

    @Autowired
    private QyxxMapper qyxxMapper;

    @Override
    public PageResult getMsggllist(MsgQueryDTO dto) {
        QueryWrapper<VRsRlzyMsgglPO> queryWrapper = new QueryWrapper<VRsRlzyMsgglPO>();
        Map<String, Object> params = new HashMap<String, Object>();

        //查询该账户所属企业流水号
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        RsRlzyQyxxPO po = qyxxMapper.selectOne(new QueryWrapper<RsRlzyQyxxPO>().select("rs_rlzy_qyxx_lsh").eq("rs_yhlsh",userid));
        if (po!=null && !("".equals(po.getRsrlzyqyxxlsh()))){
            params.put("rs_rlzy_qyxx_lsh",po.getRsrlzyqyxxlsh());
        } else {
            throw new AppException("没有该企业信息");
        }
        params.put("ts_yxbz", "1");
        Page<VRsRlzyMsgglPO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<VRsRlzyMsgglPO> pageResult = (Page) this.vRsRlzyMsgglMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public VRsRlzyMsgglPO saveMsgglInfo(MsgUDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        VRsRlzyMsgglPO po = new VRsRlzyMsgglPO();
        BeanUtils.copyProperties(dto,po);

        DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = LocalDateTime.now().format(DATETIME_FORMATTER);
        po.setTsxgr(userid);
        po.setTsxgsj(dateStr);

        if (po!=null && StringUtils.isNotBlank(po.getRsrlzymsggllsh())){
            //修改
            vRsRlzyMsgglMapper.updateById(po);
        } else {
            //新增
            po.setTscjr(userid);
            vRsRlzyMsgglMapper.insert(po);
        }
        return po;
    }

    @Override
    public void delMsggl(MsgglZtDTO dto) {
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        VRsRlzyMsgglPO po = new VRsRlzyMsgglPO();
        DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = LocalDateTime.now().format(DATETIME_FORMATTER);
        po.setRsrlzymsggllsh("");
        po.setTsxgsj(dateStr);
        po.setTsyxbz("0");
        vRsRlzyMsgglMapper.update(po,new UpdateWrapper<VRsRlzyMsgglPO>().eq("rs_rlzy_msggl_lsh",dto.getRsrlzymsggllsh().trim()));
    }

    @Override
    public VRsRlzyMsgglPO getMsgglInfo(MsgglZtDTO dto) {
        QueryWrapper<VRsRlzyMsgglPO> queryWrapper = new QueryWrapper<VRsRlzyMsgglPO>();
        VRsRlzyMsgglPO po = vRsRlzyMsgglMapper.selectOne(new QueryWrapper<VRsRlzyMsgglPO>()
                .eq("rs_rlzy_msggl",dto.getRsrlzymsggllsh()).eq("ts_yxbz", "1"));
        return po;
    }

    @Override
    public PageResult getMsgXzList(MsgglZtDTO dto) {
        return null;
    }
}



