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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import com.zjrs.zwnw.zj.blo.InitBLO;
import com.zjrs.zwnw.zj.entity.*;
import com.zjrs.zwnw.zj.po.*;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class InitBLOImpl  extends BLOImpl implements InitBLO {
    @Resource
    InitMapper initMapper;
    @Resource
    QsbsMapper qsbsMapper;
    @Resource
    QsbshMapper qsbshMapper;
    @Resource
    QstcMapper qstcMapper;
    @Resource
    QszjMapper qszjMapper;
    @Override
    public void save(InitPO initPO) {
        QueryWrapper<InitPO> queryWrapper = new QueryWrapper<>();
        int cnt=initMapper.selectCount(queryWrapper);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDate = dateFormat.format(date);
        if(cnt>0){
            UpdateWrapper<InitPO> updateWrapper=new UpdateWrapper<>();
            initMapper.update(initPO,updateWrapper);
        }else{
            initPO.setTsCjr(userid);
            initPO.setTsCjsj(nowDate);
        }
    }

    @Override
    public InitPO getInfo() {
        QueryWrapper<InitPO> queryWrapper = new QueryWrapper<>();
        InitPO initPO= initMapper.selectOne(queryWrapper);
        return initPO;
    }

    @Override
    public int queryIsTb() {
        QueryWrapper<QsbsPO> queryWrapper = new QueryWrapper<>();
        int cnt=qsbsMapper.selectCount(queryWrapper);
        if(cnt>0){
            return cnt;
        }
        QueryWrapper<QsbshPO> queryWrapper2 = new QueryWrapper<>();
        cnt=qsbshMapper.selectCount(queryWrapper2);
        if(cnt>0){
            return cnt;
        }
        QueryWrapper<QstcPO> queryWrapper3 = new QueryWrapper<>();
        cnt=qstcMapper.selectCount(queryWrapper3);
        if(cnt>0){
            return cnt;
        }
        QueryWrapper<QszjPO> queryWrapper4 = new QueryWrapper<>();
        cnt=qszjMapper.selectCount(queryWrapper4);
        if(cnt>0){
            return cnt;
        }
        return 0;
    }

    @Override
    public int getCnt() {
        QueryWrapper<InitPO> queryWrapper = new QueryWrapper<>();
        return initMapper.selectCount(queryWrapper);
    }
}
