package com.njzscloud.supervisory.tsp.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.common.security.support.UserDetail;
import com.njzscloud.common.security.util.SecurityUtil;
import com.njzscloud.supervisory.tsp.contant.PointStatus;
import com.njzscloud.supervisory.tsp.mapper.TempStoragePointMapper;
import com.njzscloud.supervisory.tsp.pojo.TempStoragePointEntity;
import com.njzscloud.supervisory.tsp.pojo.TempStoragePointStatisticsResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 临时收纳点
 */
@Slf4j
@Service
public class TempStoragePointService extends ServiceImpl<TempStoragePointMapper, TempStoragePointEntity> implements IService<TempStoragePointEntity> {

    /**
     * 新增
     *
     * @param tempStoragePointEntity 数据
     */
    public void add(TempStoragePointEntity tempStoragePointEntity) {
        this.save(tempStoragePointEntity);
    }

    /**
     * 修改
     *
     * @param tempStoragePointEntity 数据
     */
    public void modify(TempStoragePointEntity tempStoragePointEntity) {
        this.updateById(tempStoragePointEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return TempStoragePointEntity 结果
     */
    public TempStoragePointEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param tempStoragePointEntity 筛选条件
     * @param pageParam              分页参数
     * @return PageResult&lt;TempStoragePointEntity&gt; 分页结果
     */
    public PageResult<TempStoragePointEntity> paging(PageParam pageParam, TempStoragePointEntity tempStoragePointEntity) {
        UserDetail userDetail = SecurityUtil.loginUser();
        boolean isAdmin = userDetail.getRoles().contains("ROLE_ADMIN");
        Long userId = userDetail.getUserId();
        String pointName = tempStoragePointEntity.getPointName();
        PointStatus status = tempStoragePointEntity.getStatus();
        return PageResult.of(baseMapper.paging(pageParam.toPage(), Wrappers.<TempStoragePointEntity>query()
                .eq(!isAdmin, "manager_id", userId)
                .and(StrUtil.isNotBlank(pointName), it ->
                        it.like("point_name", pointName)
                                .or().like("street_name", pointName)
                                .or().like("microdistrict", pointName)
                                .or().like("property_management", pointName))

                .eq(status != null, "status", status)
                .eq("deleted", 0))
        );
    }

    public TempStoragePointStatisticsResult statistics() {
        return baseMapper.statistics();
    }
}
