package com.zjrs.ggfw.rlzysc.blo.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 com.zjrs.ggfw.portal.blo.UserBLO;
import com.zjrs.ggfw.portal.po.VRsGgfwUser;
import com.zjrs.ggfw.rlzysc.blo.JlxxBLO;
import com.zjrs.ggfw.rlzysc.bpo.JlxxBPO;
import com.zjrs.ggfw.rlzysc.dto.JlxxFileUploadDTO;
import com.zjrs.ggfw.rlzysc.dto.JlxxQueryDTO;
import com.zjrs.ggfw.rlzysc.entity.*;
import com.zjrs.ggfw.rlzysc.po.*;
import org.springframework.stereotype.Service;

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

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


@Service
public class JlxxBLOImpl implements JlxxBLO {
    @Resource
    private JlxxMapper jlxxMapper;
    @Resource
    UserBLO userBLO;

    @Override
    public PageResult getJlxxList(JlxxQueryDTO dto, int page, int rows) {
        Page<JlxxPO> page1 = new Page<>(page, rows);
        Page<JlxxPO> pageResult = null;
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf2.format(calendar.getTime()); //当前日期
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
            VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(userid);
            if(null != vRsGgfwUser){
                dto.setRsyhlsh(vRsGgfwUser.getUserid());
                dto.setFwdx(vRsGgfwUser.getFwdx());
            }
            if (null != dto.getRq() && dto.getRq() != ""){
                if(dto.getRq().equals("1")){
                    calendar.add(Calendar.DATE, -3);
                    String stq = sdf2.format(calendar.getTime());//三天前的日期
                    pageResult = (Page)this.jlxxMapper.getJlxxList(page1,dto,currentDate,stq);
                }
                if (dto.getRq().equals("5")){
                    calendar.add(Calendar.DATE, -30);
                    String sstq = sdf2.format(calendar.getTime());//其他
                    pageResult = (Page)this.jlxxMapper.getJlxxList(page1,dto,currentDate,sstq);
                }
            }else{
                pageResult = (Page)this.jlxxMapper.getJlxxList(page1,dto);
            }
            PageResult result = PageUtil.PageResult(pageResult);
            return result;
        }


    @Override
    public List<JlxxBPO> getRecentJlxxList() {
        return null;
    }

    @Override
    public void uploadFile(JlxxFileUploadDTO dto) {
        JlxxPO jlxxPO = new JlxxPO();
        jlxxPO.setRsrlzyjlxxlsh(dto.getRsrlzyjlxxlsh());
        jlxxPO.setJltycclsh(dto.getJltycclsh());
        jlxxPO.setJltyccmc(dto.getJltyccmc());
        this.jlxxMapper.updateById(jlxxPO);
    }

}



