package com.xunmei.system.service.impl;

import java.util.ArrayList;

import java.util.List;

import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.utils.bean.BeanUtils;
import com.xunmei.system.api.domain.SysArea;
import com.xunmei.system.api.domain.SysOrg;
import com.xunmei.system.domain.SysAreaCheck;

import com.xunmei.system.service.ISysAreaCheckService;
import com.xunmei.system.service.ISysAreaService;
import com.xunmei.system.service.ISysNfcBindService;

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.SysNfcBindMapper;
import com.xunmei.system.domain.SysNfcBind;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-15
 */
@Service
public class SysNfcBindServiceImpl extends ServiceImpl<SysNfcBindMapper, SysNfcBind> implements ISysNfcBindService {
    @Autowired
    private SysNfcBindMapper sysNfcBindMapper;
    @Autowired
    private ISysOrgService orgService;
    @Autowired
    private ISysOrgService sysOrgService;
    @Autowired
    private ISysAreaCheckService sysAreaCheckService;
    @Autowired
    private ISysAreaService sysAreaService;

    @Override
    public TableDataInfo<SysNfcBind> selectPage(SysNfcBind sysNfcBind) {
        //未删除
        sysNfcBind.setDelFlag("0");
        Page<SysNfcBind> page;
        //分页
        if (sysNfcBind.getPageNum() != null && sysNfcBind.getPageSize() != null) {
            page = new Page<>(sysNfcBind.getPageNum(), sysNfcBind.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysNfcBind> query = new QueryWrapper<>(sysNfcBind);
        //下穿
        if (sysNfcBind.getCheckSub()) {
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysNfcBind.getOrgId());
            //清空前端传递的org_id
            sysNfcBind.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        //时间范围查询
        if (sysNfcBind.getParams().get("beginTime") != null && sysNfcBind.getParams().get("endTime") != null) {
            query.between("create_time", sysNfcBind.getParams().get("beginTime"), sysNfcBind.getParams().get("endTime"));
        }
        //获取数据
        page = sysNfcBindMapper.selectPage(page, query);

        return TableDataInfo.build(page);


    }


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

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysNfcBind 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysNfcBind> selectSysNfcBindList(SysNfcBind sysNfcBind) {
        return sysNfcBindMapper.selectList(new QueryWrapper<>(sysNfcBind));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysNfcBind 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysNfcBind(SysNfcBind sysNfcBind) {
        sysNfcBind.setUpdateTime(DateUtils.getNowDate());
        sysNfcBind.setCreateTime(DateUtils.getNowDate());
        sysNfcBind.setDelFlag("0");
        SysOrg sysOrg = sysOrgService.selectSysOrgById(sysNfcBind.getOrgId());
        if (null != sysOrg) {
            sysNfcBind.setOrgName(sysOrg.getName());
        }
        SysAreaCheck areaCheck = sysAreaCheckService.selectSysAreaCheckById(sysNfcBind.getCheckId());
        if (null != areaCheck) {
            sysNfcBind.setCollectionAreaName(areaCheck.getCheckName());
        }
        SysArea sysArea = sysAreaService.selectSysAreaById(sysNfcBind.getAreaId());
        if (null != sysArea) {
            sysNfcBind.setAreaName(sysArea.getName());
        }
        return sysNfcBindMapper.insert(sysNfcBind);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysNfcBind 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysNfcBind(SysNfcBind sysNfcBind) {
        sysNfcBind.setUpdateTime(DateUtils.getNowDate());
        SysOrg sysOrg = sysOrgService.selectSysOrgById(sysNfcBind.getOrgId());
        if (null != sysOrg) {
            sysNfcBind.setOrgName(sysOrg.getName());
        }
        SysAreaCheck areaCheck = sysAreaCheckService.selectSysAreaCheckById(sysNfcBind.getCheckId());
        if (null != areaCheck) {
            sysNfcBind.setCollectionAreaName(areaCheck.getCheckName());
        }
        SysArea sysArea = sysAreaService.selectSysAreaById(sysNfcBind.getAreaId());
        if (null != sysArea) {
            sysNfcBind.setAreaName(sysArea.getName());
        }
        return sysNfcBindMapper.updateById(sysNfcBind);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysNfcBindByIds(Long[] ids) {
        int i = 0;
        for (Long id : ids) {
            SysNfcBind sysNfcBind = new SysNfcBind();
            sysNfcBind.setDelFlag("2");
            sysNfcBind.setId(id);
            i = sysNfcBindMapper.updateSysNfcBind(sysNfcBind);
            if (i < 1) {
                return i;
            }
        }
        return i;

    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysNfcBindById(Long id) {
        return sysNfcBindMapper.deleteById(id);
    }
}
