package com.mosty.qwzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.dto.qwzx.TbQwBxfsqEditDto;
import com.mosty.base.model.entity.qwzx.TbQwXfBxfsq;
import com.mosty.base.model.query.qwzx.TbQwXfBxfsqQuery;
import com.mosty.qwzx.mapper.TbQwXfBxfsqMapper;
import com.mosty.qwzx.remote.TbBaseAdaptRemoteService;
import com.mosty.qwzx.service.TbQwXfBxfsqService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@Service
public class TbQwXfBxfsqServiceImpl extends ServiceImpl<TbQwXfBxfsqMapper, TbQwXfBxfsq>
        implements TbQwXfBxfsqService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int addBxfsq(TbQwXfBxfsq bxfsq) {
        bxfsq.setId(UUIDGenerator.getUUID());
        if (StringUtils.isBlank(bxfsq.getSqbmid())) throw new BusinessException("请选择申请部门！");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(bxfsq.getSqbmid());
        bxfsq.setSsbm(dept.getDeptname());
        bxfsq.setSsbmid(dept.getDeptid());
        bxfsq.setSsbmdm(dept.getDeptcode());
        bxfsq.setSsxgajdm(dept.getFxjcode());
        bxfsq.setSsxgajid(dept.getFxjid());
        bxfsq.setSsxgaj(dept.getFxjname());
        bxfsq.setSssgaj(dept.getDszname());
        bxfsq.setSssgajdm(dept.getDszcode());
        bxfsq.setSssgajid(dept.getDszid());
        bxfsq.setXtSjly("1");
        bxfsq.setSqsj(new Date());
        bxfsq.setShzt("0");
        return this.baseMapper.insert(bxfsq);
    }

    @Override
    public IPage<TbQwXfBxfsq> getBxfsq(TbQwXfBxfsqQuery bxfsq, IPage<TbQwXfBxfsq> page) {
        QueryWrapper<TbQwXfBxfsq> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        bxfsq.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(bxfsq.getSsbmdm(), null));

        qw.lambda().eq(StringUtils.isNotBlank(bxfsq.getXfpbId()), TbQwXfBxfsq::getXfpbId, bxfsq.getXfpbId())
                .like(StringUtils.isNotBlank(bxfsq.getSqrXm()), TbQwXfBxfsq::getSqrXm, bxfsq.getSqrXm())
                .likeRight(StringUtils.isNotBlank(bxfsq.getSsbmdm()), TbQwXfBxfsq::getSsbmdm, bxfsq.getSsbmdm())
                .eq(StringUtils.isNotBlank(bxfsq.getSqbmdm()), TbQwXfBxfsq::getSqbmdm, bxfsq.getSqbmdm())
                .eq(StringUtils.isNotBlank(bxfsq.getSqbmid()), TbQwXfBxfsq::getSqbmid, bxfsq.getSqbmid())
                .eq(StringUtils.isNotBlank(bxfsq.getShbmdm()), TbQwXfBxfsq::getShbmdm, bxfsq.getShbmdm())
                .eq(StringUtils.isNotBlank(bxfsq.getShbmid()), TbQwXfBxfsq::getShbmid, bxfsq.getShbmid())
                .eq(StringUtils.isNotBlank(bxfsq.getShzt()), TbQwXfBxfsq::getShzt, bxfsq.getShzt())
                .ge(bxfsq.getKssj() != null, TbQwXfBxfsq::getBxfsj, bxfsq.getKssj())
                .le(bxfsq.getJssj() != null, TbQwXfBxfsq::getBxfsj, bxfsq.getJssj());
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public int editBxfsq(TbQwXfBxfsq bxfsq) {
        TbQwXfBxfsq item = new TbQwXfBxfsq();
        BeanUtils.copyProperties(bxfsq, item);
        if (StringUtils.isBlank(bxfsq.getSqbmid())) throw new BusinessException("请选择申请部门！");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(bxfsq.getSqbmid());
        item.setSsbm(dept.getDeptname());
        item.setSsbmid(dept.getDeptid());
        item.setSsbmdm(dept.getDeptcode());
        item.setSsxgajdm(dept.getFxjcode());
        item.setSsxgajid(dept.getFxjid());
        item.setSsxgaj(dept.getFxjname());
        item.setSssgaj(dept.getDszname());
        item.setSssgajdm(dept.getDszcode());
        item.setSssgajid(dept.getDszid());
        return this.baseMapper.updateById(item);
    }

    @Override
    public boolean deleteBatch(List<String> ids) {
        if (ids.size() > 0) {
            int rs = this.baseMapper.deleteBxfsq(ids);
            return rs > 0;
        }
        return true;
    }

    @Override
    public boolean deleteById(String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        int rs = this.baseMapper.deleteBxfsq(ids);
        return rs > 0;
    }

    @Override
    public TbQwXfBxfsq selectById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int judge(TbQwBxfsqEditDto dto) {
        TbQwXfBxfsq item = new TbQwXfBxfsq();
        BeanUtils.copyProperties(dto, item);
        UserInfo user = UserInfoManager.get();
        item.setShrId(String.valueOf(user.getUserId()));
        item.setShrXm(user.getUserName());
        item.setShbm(user.getDeptName());
        item.setShbmid(String.valueOf(user.getDeptId()));
        item.setShbmdm(user.getDeptCode());
        item.setSpsj(new Date());
        return this.baseMapper.updateById(item);
    }
}
