package com.zjrs.ggfw.rlzysc.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.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.portal.entity.VRsGgfwUserMapper;
import com.zjrs.ggfw.portal.po.VRsGgfwUser;
import com.zjrs.ggfw.rlzysc.blo.JyzxBLO;
import com.zjrs.ggfw.rlzysc.dto.JyzdAddDTO;
import com.zjrs.ggfw.rlzysc.dto.JyzxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.VRsRlzyJyzxFjxxDTO;
import com.zjrs.ggfw.rlzysc.dto.YrzdAddDTO;
import com.zjrs.ggfw.rlzysc.entity.JyzdMapper;
import com.zjrs.ggfw.rlzysc.entity.JyzxMapper;
import com.zjrs.ggfw.rlzysc.po.JyzdPO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyFwggPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

import static org.mohrss.leaf.core.framework.util.SecurityUtils.getCurrentUser;

@Service
public class JyzxBLOImpl implements JyzxBLO {
    @Resource
    private JyzdMapper jyzdMapper;
    @Resource
    private JyzxMapper jyzxMapper;
    @Resource
    private VRsGgfwUserMapper vRsGgfwUserMapper;


    @Override
    public PageResult getJyzxList(JyzxQueryDTO dto, int page, int rows) {
        QueryWrapper<RsRlzyFwggPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TS_YXBZ","1");//1为有效
        queryWrapper.eq("fbzt","1");//1为已发布
        Page<RsRlzyFwggPO> page1 = new Page<>(page, rows);
        Page<RsRlzyFwggPO> pageResult = (Page) this.jyzxMapper.selectPage(page1,queryWrapper);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public RsRlzyFwggPO getJyzxDetail(String id) {
        QueryWrapper<RsRlzyFwggPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TS_YXBZ","1");//1为有效
        RsRlzyFwggPO jyzxpo = this.jyzxMapper.selectOne(queryWrapper);
        return jyzxpo;
    }

    @Override
    public void addJyzd(JyzdAddDTO dto) {
        JyzdPO po = new JyzdPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "1" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)){
            BeanUtils.copyProperties(dto, po);
            po.setRsrlzyjyzdlx(dto.getJyzdlx());
            po.setTscjr(userid);
            jyzdMapper.insert(po);
        }else{
            throw new AppException("获取不到该用户信息");
        }
    }

    @Override
    public List<VRsRlzyJyzxFjxxDTO> getJyzxFjxxForList(String id) {
        return jyzxMapper.getJyzxFjxxForList(id);
    }
    @Override
    public void addYrzd(YrzdAddDTO dto){
        JyzdPO po = new JyzdPO();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataString = dateFormat.format(date);
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "1" : currentUser.getUsername().trim();
        QueryWrapper<VRsGgfwUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("userid", userid);
        VRsGgfwUser user = vRsGgfwUserMapper.selectOne(userQueryWrapper);

        if (StringUtils.isNotBlank(userid)){
            BeanUtils.copyProperties(dto, po);
            po.setRsrlzyjyzdlx(dto.getJyzdlx());
            po.setXm(user.getMc());
            po.setSfzxx(user.getZjhm());
            jyzdMapper.insert(po);
        }else{
            throw new AppException("获取不到该用户信息");
        }
    }
}



