package com.xunmei.system.service.impl;

import java.util.List;

import com.xunmei.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunmei.system.service.ISysOrgService;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.system.mapper.SysPhysicalDefenseConstructionMapper;
import com.xunmei.system.domain.SysPhysicalDefenseConstruction;
import com.xunmei.system.service.ISysPhysicalDefenseConstructionService;

/**
 * 机构物防建设Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-16
 */
@Service
public class SysPhysicalDefenseConstructionServiceImpl extends ServiceImpl<SysPhysicalDefenseConstructionMapper, SysPhysicalDefenseConstruction> implements ISysPhysicalDefenseConstructionService {
    @Autowired
    private SysPhysicalDefenseConstructionMapper sysPhysicalDefenseConstructionMapper;
    @Autowired
    private ISysOrgService orgService;

    @Override
    public TableDataInfo<SysPhysicalDefenseConstruction> selectPage(SysPhysicalDefenseConstruction sysPhysicalDefenseConstruction) {


        //未删除
//        sysPhysicalDefenseConstruction.setDeleted(0L);
        Page<SysPhysicalDefenseConstruction> page;
        //分页
        if (sysPhysicalDefenseConstruction.getPageNum() != null && sysPhysicalDefenseConstruction.getPageSize() != null) {
            page = new Page<>(sysPhysicalDefenseConstruction.getPageNum(), sysPhysicalDefenseConstruction.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysPhysicalDefenseConstruction> query = new QueryWrapper<>(sysPhysicalDefenseConstruction);
        //下穿
        if (sysPhysicalDefenseConstruction.getCheckSub()) {
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysPhysicalDefenseConstruction.getOrgId());
            //清空前端传递的org_id
            sysPhysicalDefenseConstruction.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        //时间范围查询
        if (sysPhysicalDefenseConstruction.getParams().get("beginTime") != null && sysPhysicalDefenseConstruction.getParams().get("endTime") != null) {
            query.between("create_time", sysPhysicalDefenseConstruction.getParams().get("beginTime"), sysPhysicalDefenseConstruction.getParams().get("endTime"));
        }
        //获取数据
        page = sysPhysicalDefenseConstructionMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);


    }


    /**
     * 查询机构物防建设
     *
     * @param id 机构物防建设主键
     * @return 机构物防建设
     */
    @Override
    public SysPhysicalDefenseConstruction selectSysPhysicalDefenseConstructionById(Long id) {
        return sysPhysicalDefenseConstructionMapper.selectById(id);
    }

    /**
     * 查询机构物防建设列表
     *
     * @param sysPhysicalDefenseConstruction 机构物防建设
     * @return 机构物防建设
     */
    @Override
    public List<SysPhysicalDefenseConstruction> selectSysPhysicalDefenseConstructionList(SysPhysicalDefenseConstruction sysPhysicalDefenseConstruction) {
        return sysPhysicalDefenseConstructionMapper.selectList(new QueryWrapper<>(sysPhysicalDefenseConstruction));
    }

    /**
     * 新增机构物防建设
     *
     * @param sysPhysicalDefenseConstruction 机构物防建设
     * @return 结果
     */
    @Override
    public int insertSysPhysicalDefenseConstruction(SysPhysicalDefenseConstruction sysPhysicalDefenseConstruction) {
        sysPhysicalDefenseConstruction.setCreateTime(DateUtils.getNowDate());
        return sysPhysicalDefenseConstructionMapper.insert(sysPhysicalDefenseConstruction);
    }

    /**
     * 修改机构物防建设
     *
     * @param sysPhysicalDefenseConstruction 机构物防建设
     * @return 结果
     */
    @Override
    public int updateSysPhysicalDefenseConstruction(SysPhysicalDefenseConstruction sysPhysicalDefenseConstruction) {
        sysPhysicalDefenseConstruction.setUpdateTime(DateUtils.getNowDate());
        return sysPhysicalDefenseConstructionMapper.updateById(sysPhysicalDefenseConstruction);
    }

    /**
     * 批量删除机构物防建设
     *
     * @param ids 需要删除的机构物防建设主键
     * @return 结果
     */
    @Override
    public int deleteSysPhysicalDefenseConstructionByIds(Long[] ids) {
        return sysPhysicalDefenseConstructionMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除机构物防建设信息
     *
     * @param id 机构物防建设主键
     * @return 结果
     */
    @Override
    public int deleteSysPhysicalDefenseConstructionById(Long id) {
        return sysPhysicalDefenseConstructionMapper.deleteById(id);
    }
}
