package com.mosty.spxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.base.model.dto.spxl.TbSpxlSpxlfaUpdateDto;
import com.mosty.base.model.entity.spxl.TbSpxlSpxlfa;
import com.mosty.base.model.query.spxl.TbSpxlSpxlfaQuery;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.spxl.mapper.TbSpxlSpxlfaMapper;
import com.mosty.spxl.remote.TbBaseAdaptRemoteService;
import com.mosty.spxl.service.TbSpxlSpxlfaService;
import com.mosty.spxl.service.TbSpxlSpxlfaSxtService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author dw
 * @since 2022/7/11
 * 视频巡逻方案服务实现类
 **/
@AllArgsConstructor
@Service
public class TbSpxlSpxlfaServiceImpl extends ServiceImpl<TbSpxlSpxlfaMapper, TbSpxlSpxlfa>
        implements TbSpxlSpxlfaService {

    private final  TbSpxlSpxlfaSxtService tbSpxlSpxlfaSxtService;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    @Override
    public int addEntity(TbSpxlSpxlfa fa) {
        UserInfo user = UserInfoManager.get();
        if (user != null) {
            fa.setCjrid(String.valueOf(user.getUserId()));
            fa.setCjrsfzh(user.getIdEntityCard());
            fa.setCjrxm(user.getUserName());
            fa.setSsbm(user.getDeptName());
            fa.setSsbmid(String.valueOf(user.getDeptId()));
            fa.setSsbmdm(user.getDeptCode());
            fa.setSsxgaj(user.getFxjDeptName());
            fa.setSsxgajid(String.valueOf(user.getFxjDeptId()));
            fa.setSsxgajdm(user.getFxjDeptCode());
            fa.setSssgaj(user.getDszDeptName());
            fa.setSssgajid(String.valueOf(user.getDszDeptId()));
            fa.setSssgajdm(user.getDszDeptCode());
        }
        fa.setXtSjly("1");
        return this.baseMapper.insert(fa);
    }

    @Override
    @Transactional
    public int delEntity(List<String> ids) {
        return this.baseMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateEntity(TbSpxlSpxlfaUpdateDto dto) {
        TbSpxlSpxlfa fa = this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbSpxlSpxlfa>()
                        .eq(TbSpxlSpxlfa::getXtSjzt, "1")
                        .eq(TbSpxlSpxlfa::getXtScbz, "0")
                        .eq(TbSpxlSpxlfa::getId, dto.getId())
        );
        if (fa == null) {
            throw new BusinessException("方案不存在或已被删除");
        }
        TbSpxlSpxlfa entity = new TbSpxlSpxlfa();
        BeanUtils.copyProperties(dto, entity);
        return this.baseMapper.updateById(entity);
    }

    @Override
    public IPage<TbSpxlSpxlfa> queryPage(TbSpxlSpxlfaQuery dto) {
        QueryWrapper<TbSpxlSpxlfa> queryWrapper = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        return this.baseMapper.selectPage(
                new Page<>(dto.getPageNum(), dto.getPageSize()),
                queryWrapper.lambda()
                        .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbSpxlSpxlfa::getSsbmdm, dto.getSsbmdm())
                        .like(StringUtils.isNotBlank(dto.getFamc()), TbSpxlSpxlfa::getFamc, dto.getFamc())
                        .eq(StringUtils.isNotBlank(dto.getFalx()), TbSpxlSpxlfa::getFalx, dto.getFalx())
                        .eq(StringUtils.isNotBlank(dto.getFps()), TbSpxlSpxlfa::getFps, dto.getFps())
                        .le(StringUtils.isNotBlank(dto.getJssj()), TbSpxlSpxlfa::getJssj, dto.getJssj())
                        .le(StringUtils.isNotBlank(dto.getSsbmid()), TbSpxlSpxlfa::getSsbmid, dto.getSsbmid())
                        .ge(StringUtils.isNotBlank(dto.getKssj()), TbSpxlSpxlfa::getKssj, dto.getKssj())
                        .orderByDesc(TbSpxlSpxlfa::getXtCjsj)
        );
    }

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