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.GxygBLO;
import com.zjrs.ggfw.rlzysc.dto.GxygDTO;
import com.zjrs.ggfw.rlzysc.dto.GxygQueryDTO;
import com.zjrs.ggfw.rlzysc.entity.GxygMapper;
import com.zjrs.ggfw.rlzysc.entity.QyxxMapper;
import com.zjrs.ggfw.rlzysc.po.RsRlzyGxygPO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyQyxxPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class GxygBLOImpl extends BLOImpl implements GxygBLO {

    private static final Logger LOG = LoggerFactory.getLogger(GxygBLOImpl.class);

    @Resource
    private GxygMapper gxygMapper;

    @Resource
    private QyxxMapper qyxxMapper;


    @Override
    public PageResult queryGxygList(GxygQueryDTO dto, Integer pagenum, Integer pagesize) {
        Page<GxygDTO> page = new Page<>(pagenum, pagesize);
        Page<GxygDTO> pageResult = null;
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        dto.setRsyhlsh(userid);

        if("1".equals(dto.getType())) {
            pageResult = (Page) this.gxygMapper.getAllGxygList(page, dto);
        } else {
            pageResult = (Page) this.gxygMapper.getGxygList(page, dto);
        }
        return PageUtil.PageResult(pageResult);
    }

    @Override
    public RsRlzyGxygPO queryGxyg(GxygDTO dto) {
        QueryWrapper<RsRlzyGxygPO> queryWrapper = new QueryWrapper<>();
        return this.gxygMapper.selectOne(queryWrapper);
    }

    @Override
    public String saveOrUpdateGxyg(GxygDTO dto) {
        String rsrlzygxyglsh = dto.getRsrlzygxyglsh();
        RsRlzyGxygPO gxygPo = new RsRlzyGxygPO();
        BeanUtils.copyProperties(dto, gxygPo);

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();

        if(StringUtils.isNotBlank(rsrlzygxyglsh)) {
            QueryWrapper<RsRlzyGxygPO> queryWrapper = new QueryWrapper<>();
            RsRlzyGxygPO po = this.gxygMapper.selectOne(queryWrapper);
            if(po != null) {
                throw new AppException("该共享用工信息已提交且发布, 无法进行修改");
            }
            gxygPo.setFbzt("0");
            gxygPo.setShzt("");
            this.gxygMapper.updateById(gxygPo);
        } else {
            QueryWrapper<RsRlzyQyxxPO> qyxxPOQueryWrapper = new QueryWrapper<>();
            qyxxPOQueryWrapper.eq("rs_yhlsh", userid);
            RsRlzyQyxxPO qyxxPO = this.qyxxMapper.selectOne(qyxxPOQueryWrapper);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dataString = dateFormat.format(new Date());
            gxygPo.setTsop(userid);
            gxygPo.setTsot(dataString);

            this.gxygMapper.insert(gxygPo);
        }

        return gxygPo.getRsrlzygxyglsh();
    }

    @Override
    public void delete(GxygDTO dto) {
        QueryWrapper<RsRlzyGxygPO> queryWrapper = new QueryWrapper<>();
        String rsrlzygxyglsh = dto.getRsrlzygxyglsh();
        queryWrapper.in("rs_rlzy_gxyg", rsrlzygxyglsh.split(","))
                .and(q -> q.eq("shzt", "1"));
        List<RsRlzyGxygPO> list = this.gxygMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)) {
            throw new AppException("存在已审核或已发布的共享用工信息, 无法进行删除");
        }

        UpdateWrapper<RsRlzyGxygPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("ts_yxbz", "0");
        updateWrapper.in("rs_rlzy_gxyg_", rsrlzygxyglsh.split(","));
        gxygMapper.update(new RsRlzyGxygPO(), updateWrapper);
    }

    @Override
    public void submit(GxygDTO dto) {

        RsRlzyGxygPO gxygPo = new RsRlzyGxygPO();
        BeanUtils.copyProperties(dto, gxygPo);
        String rsrlzygxyglsh = dto.getRsrlzygxyglsh();
        UpdateWrapper<RsRlzyGxygPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("shzt", "0");
        this.gxygMapper.update(gxygPo, updateWrapper);
    }

    @Override
    public void release(GxygDTO dto) {
        RsRlzyGxygPO gxygPo = new RsRlzyGxygPO();

        QueryWrapper<RsRlzyGxygPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rs_rlzy_gxyg", dto.getRsrlzygxyglsh()).eq("zxzt", "0");
        RsRlzyGxygPO gxygPO = this.gxygMapper.selectOne(queryWrapper);

        UpdateWrapper<RsRlzyGxygPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("fbzt", dto.getFbzt());
        this.gxygMapper.update(gxygPo, updateWrapper);
    }

    @Override
    public String status() {
        return this.gxygMapper.getGxygStatus();
    }
}



