package com.xunmei.system.service.impl;

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

import com.xunmei.common.core.exception.ServiceException;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.common.core.utils.bean.BeanUtils;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.system.api.domain.SysArea;
import com.xunmei.system.api.domain.SysOrg;
import com.xunmei.system.domain.SysNfcBind;
import com.xunmei.system.domain.vo.SysAreaCheckVO;
import com.xunmei.system.mapper.ISysAreaMapper;
import com.xunmei.system.mapper.SysNfcBindMapper;
import com.xunmei.system.mapper.SysOrgMapper;
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.SysAreaCheckMapper;
import com.xunmei.system.domain.SysAreaCheck;
import com.xunmei.system.service.ISysAreaCheckService;

/**
 * 区域采集点Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-15
 */
@Service
public class SysAreaCheckServiceImpl extends ServiceImpl<SysAreaCheckMapper, SysAreaCheck> implements ISysAreaCheckService {
    @Autowired
    private SysAreaCheckMapper sysAreaCheckMapper;
    @Autowired
    private ISysOrgService orgService;
    @Autowired
    private ISysAreaMapper sysAreaMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private SysNfcBindMapper sysNfcBindMapper;

    @Override
    public TableDataInfo<SysAreaCheck> selectPage(SysAreaCheck sysAreaCheck) {
        //未删除
        sysAreaCheck.setDelFlag("0");
        Page<SysAreaCheck> page;
        //分页
        if (sysAreaCheck.getPageNum() != null && sysAreaCheck.getPageSize() != null) {
            page = new Page<>(sysAreaCheck.getPageNum(), sysAreaCheck.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysAreaCheck> query = new QueryWrapper<>(sysAreaCheck);
        //下穿
        //先全部查出
        sysAreaCheck.setCheckSub(true);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_id",-1);
        SysOrg sysOrg1 = sysOrgMapper.selectOne(queryWrapper);
        if (null==sysAreaCheck.getOrgId()){
            sysAreaCheck.setOrgId(sysOrg1.getId());
        }
        if (sysAreaCheck.getCheckSub()) {
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysAreaCheck.getOrgId());
            //清空前端传递的org_id
            sysAreaCheck.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        //时间范围查询
        if (sysAreaCheck.getParams().get("beginTime") != null && sysAreaCheck.getParams().get("endTime") != null) {
            query.between("create_time", sysAreaCheck.getParams().get("beginTime"), sysAreaCheck.getParams().get("endTime"));
        }
        //获取数据
        page = sysAreaCheckMapper.selectPage(page, query);

        List<SysAreaCheckVO> list = new ArrayList<>();
        List<SysAreaCheck> records = page.getRecords();
        for (SysAreaCheck s : records) {
            SysAreaCheckVO sysAreaCheckVO = new SysAreaCheckVO();
            BeanUtils.copyProperties(s, sysAreaCheckVO);
            SysArea sysArea = sysAreaMapper.selectSysAreaById(s.getAreaId());
            SysOrg sysOrg = sysOrgMapper.selectSysOrgById(s.getOrgId());
            if (null != sysArea) {
                sysAreaCheckVO.setAreaName(sysArea.getName());
            }
            sysAreaCheckVO.setOrgName(sysOrg.getName());
            list.add(sysAreaCheckVO);
        }
        return TableDataInfo.build(page);


    }

    @Override
    public List<SysAreaCheck> selectByAreaId(Long areaId) {
        SysAreaCheck areaCheck = new SysAreaCheck();
        areaCheck.setAreaId(areaId);
        areaCheck.setDelFlag("0");
        //查询机构名称
        return baseMapper.selectSysAreaCheckList(areaCheck);
    }


    /**
     * 查询区域采集点
     *
     * @param id 区域采集点主键
     * @return 区域采集点
     */
    @Override
    public SysAreaCheck selectSysAreaCheckById(Long id) {
        return sysAreaCheckMapper.selectById(id);
    }

    /**
     * 查询区域采集点列表
     *
     * @param sysAreaCheck 区域采集点
     * @return 区域采集点
     */
    @Override
    public List<SysAreaCheck> selectSysAreaCheckList(SysAreaCheck sysAreaCheck) {
        sysAreaCheck.setDelFlag("0");
        return sysAreaCheckMapper.selectList(new QueryWrapper<>(sysAreaCheck));
    }

    /**
     * 新增区域采集点
     *
     * @param sysAreaCheck 区域采集点
     * @return 结果
     */
    @Override
    public int insertSysAreaCheck(SysAreaCheck sysAreaCheck) {
        sysAreaCheck.setCreateTime(DateUtils.getNowDate());
        sysAreaCheck.setCreateBy(SecurityUtils.getUsername());
        sysAreaCheck.setUpdateBy(SecurityUtils.getUsername());
        sysAreaCheck.setUpdateTime(DateUtils.getNowDate());
        sysAreaCheck.setUpdateId(SecurityUtils.getUserId().toString());
        return sysAreaCheckMapper.insert(sysAreaCheck);
    }

    /**
     * 修改区域采集点
     *
     * @param sysAreaCheck 区域采集点
     * @return 结果
     */
    @Override
    public int updateSysAreaCheck(SysAreaCheck sysAreaCheck) {
        sysAreaCheck.setUpdateBy(SecurityUtils.getUsername());
        sysAreaCheck.setUpdateTime(DateUtils.getNowDate());
        sysAreaCheck.setUpdateId(SecurityUtils.getUserId().toString());
        return sysAreaCheckMapper.updateById(sysAreaCheck);
    }

    /**
     * 批量删除区域采集点
     *
     * @param ids 需要删除的区域采集点主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaCheckByIds(Long[] ids) {
        //判断是否使用
        QueryWrapper<SysNfcBind> query = new QueryWrapper<>(new SysNfcBind());
        query.eq("del_flag", "0");
        query.in("check_id", ids);
        List<SysNfcBind> sysNfcBinds = sysNfcBindMapper.selectList(query);
        if (null == sysNfcBinds || sysNfcBinds.size() > 0) {
            throw new ServiceException("数据使用中，不能删除");
        }
        int i = 0;
        for (Long id : ids) {
            SysAreaCheck sysAreaCheck = new SysAreaCheck();
            sysAreaCheck.setDelFlag("2");
            sysAreaCheck.setId(id);
            i = sysAreaCheckMapper.updateSysAreaCheck(sysAreaCheck);
            if (i < 1) {
                return i;
            }
        }
        return i;
    }

    /**
     * 删除区域采集点信息
     *
     * @param id 区域采集点主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaCheckById(Long id) {
        return sysAreaCheckMapper.deleteById(id);
    }
}
