package com.xunmei.system.service.impl;

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

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.xunmei.common.core.exception.ServiceException;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.system.api.domain.SysArea;
import com.xunmei.system.api.domain.SysDictData;
import com.xunmei.system.api.model.LoginUser;
import com.xunmei.system.domain.SysAreaCheck;
import com.xunmei.system.mapper.SysAreaCheckMapper;
import com.xunmei.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunmei.system.service.ISysOrgService;

import java.util.Arrays;
import java.util.stream.Collectors;

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.ISysAreaMapper;
import com.xunmei.system.service.ISysAreaService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-14
 */
@Service
public class SysAreaServiceImpl extends ServiceImpl<ISysAreaMapper, SysArea> implements ISysAreaService {
    @Autowired
    private ISysAreaMapper sysAreaMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysAreaCheckMapper sysAreaCheckMapper;

    @Override
    public TableDataInfo<SysArea> selectPage(SysArea sysArea) {


        //未删除
        sysArea.setDelFlag(0L);
        Page<SysArea> page;
        //分页
        if (sysArea.getPageNum() != null && sysArea.getPageSize() != null) {
            page = new Page<>(sysArea.getPageNum(), sysArea.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysArea> query = new QueryWrapper<>(sysArea);
        //下穿
        if (sysArea.getCheckSub()) {
            //  List<Long> ids = orgService.selectCheckSubOrgIdList(sysArea.getOrgType());
            List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType(sysArea.getOrgType());
            List<String> ids = sysDictData.stream().map(e -> e.getDictValue()).collect(Collectors.toList());
            //清空前端传递的org_id
            sysArea.setOrgType(null);
            //添加in条件
            query.in("org_type", ids);
        }
        //时间范围查询
        if (sysArea.getParams().get("beginTime") != null && sysArea.getParams().get("endTime") != null) {
            query.between("create_time", sysArea.getParams().get("beginTime"), sysArea.getParams().get("endTime"));
        }
        //获取数据
        page = sysAreaMapper.selectPage(page, query);
        page.getRecords().forEach(e -> {
            String dictLabel = sysDictDataMapper.selectDictLabel("sys_org_type", e.getOrgType());
            e.setOrgType(dictLabel);
        });

        return TableDataInfo.build(page);


    }


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysArea selectSysAreaById(Long id) {
        return sysAreaMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysArea 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea) {
        sysArea.setDelFlag(0L);
        return sysAreaMapper.selectList(new QueryWrapper<>(sysArea));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea) {
        sysArea.setCreateTime(DateUtils.getNowDate());
        sysArea.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysArea.setCreateBy(loginUser.getUsername());
        sysArea.setUpdateBy(loginUser.getUsername());
        sysArea.setUpdateId(loginUser.getUserid());
        return sysAreaMapper.insert(sysArea);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea) {
        sysArea.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysArea.setUpdateBy(loginUser.getUsername());
        sysArea.setUpdateId(loginUser.getUserid());
        return sysAreaMapper.updateById(sysArea);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(Long[] ids) {
        //查询是否有采集点在用
        SysAreaCheck sysAreaCheck = new SysAreaCheck();
        QueryWrapper<SysAreaCheck> query = new QueryWrapper<>(sysAreaCheck);
        query.eq("del_flag", "0");
        List<SysAreaCheck> sysAreaChecks = new ArrayList<>();
        query.in("area_id", ids);
        sysAreaChecks = sysAreaCheckMapper.selectList(query);
        if (null == sysAreaChecks || sysAreaChecks.size() > 0) {
            throw new ServiceException("数据使用中，不能删除");
        }
        int i = 0;
        for (Long id : ids) {
            SysArea area = new SysArea();
            area.setDelFlag(2L);
            area.setId(id);
            i = sysAreaMapper.updateSysArea(area);
            if (i < 1) {
                return i;
            }
        }
        return i;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long id) {
        //查询是否有采集点在用
        SysAreaCheck sysAreaCheck = new SysAreaCheck();
        QueryWrapper<SysAreaCheck> query = new QueryWrapper<>(sysAreaCheck);
        query.eq("del_flag", "0");
        List<SysAreaCheck> sysAreaChecks = new ArrayList<>();
        query.eq("area_id", id);
        sysAreaChecks = sysAreaCheckMapper.selectList(query);
        if (null == sysAreaChecks || sysAreaChecks.size() > 0) {
            throw new ServiceException("数据使用中，不能删除");
        }
        return sysAreaMapper.deleteById(id);
    }
}
