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.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 com.zjrs.zwnw.rlzysc.blo.SyzdqyglBLO;
import com.zjrs.zwnw.rlzysc.dto.SyzdqyglQueryDTO;
import com.zjrs.zwnw.rlzysc.dto.ZjUDTO;
import com.zjrs.zwnw.rlzysc.entity.VRsRlzyQytjMapper;
import com.zjrs.zwnw.rlzysc.po.VRsRlzyQytjPO;
import org.springframework.stereotype.Service;

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

@Service
public class SyzdqyglBLOimpl extends BLOImpl implements SyzdqyglBLO {

    @Resource
    private VRsRlzyQytjMapper vRsRlzyQytjMapper;

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

    @Override
    public void updateSyzdqyglTjzt(ZjUDTO dto) {
        VRsRlzyQytjPO po = new VRsRlzyQytjPO();
        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.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        //首先要查询企业推荐表里是否已经有数据
        int cnt = vRsRlzyQytjMapper.selectCount(new QueryWrapper<VRsRlzyQytjPO>()
                .eq("rs_rlzy_qyxx_lsh",dto.getZjlsh().trim()));
        if ("1".equals(dto.getFlag().trim())){//推荐
            if (cnt==1){
                //更新有效标志
                po.setTsyxbz("1");
                vRsRlzyQytjMapper.update(po,new UpdateWrapper<VRsRlzyQytjPO>()
                        .eq("rs_rlzy_qyxx",dto.getZjlsh().trim()));
            } 
        } else if ("0".equals(dto.getFlag().trim())){//取消推荐
            //更新有效标志
            po.setTsyxbz("0");
            vRsRlzyQytjMapper.update(po,new UpdateWrapper<VRsRlzyQytjPO>()
                    .eq("rs_rlzy_qyxx",dto.getZjlsh().trim()));
        }
    }
}
