package cn.iocoder.yudao.module.pms.service.inspect;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.*;
import cn.iocoder.yudao.module.pms.controller.admin.stat.DeptUtil;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.inspect.IotInspectOrderDetailDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcountdata.IotCountDataDO;
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.inspect.IotInspectOrderDetailMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.inspect.IotInspectOrderMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_INSPECT_ORDER_DETAIL_NOT_EXISTS;

/**
 * 巡检工单巡检明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class IotInspectOrderDetailServiceImpl implements IotInspectOrderDetailService {

    @Resource
    private IotInspectOrderDetailMapper iotInspectOrderDetailMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private IotDeviceMapper iotDeviceMapper;
    @Autowired
    private IotInspectOrderMapper iotInspectOrderMapper;
    @Autowired
    private DeptUtil deptUtil;

    @Override
    public Long createIotInspectOrderDetail(IotInspectOrderDetailSaveReqVO createReqVO) {
        // 插入
        IotInspectOrderDetailDO iotInspectOrderDetail = BeanUtils.toBean(createReqVO, IotInspectOrderDetailDO.class);
        iotInspectOrderDetailMapper.insert(iotInspectOrderDetail);
        // 返回
        return iotInspectOrderDetail.getId();
    }

    @Override
    public void updateIotInspectOrderDetail(IotInspectOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateIotInspectOrderDetailExists(updateReqVO.getId());
        // 更新
        IotInspectOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, IotInspectOrderDetailDO.class);
        iotInspectOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotInspectOrderDetail(Long id) {
        // 校验存在
        validateIotInspectOrderDetailExists(id);
        // 删除
        iotInspectOrderDetailMapper.deleteById(id);
    }

    private void validateIotInspectOrderDetailExists(Long id) {
        if (iotInspectOrderDetailMapper.selectById(id) == null) {
            throw exception(IOT_INSPECT_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public IotInspectOrderDetailDO getIotInspectOrderDetail(Long id) {
        return iotInspectOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<IotInspectOrderDetailDO> getIotInspectOrderDetailPage(IotInspectOrderDetailPageReqVO pageReqVO) {
        return iotInspectOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotInspectOrderDetailDO> getAll() {
        return iotInspectOrderDetailMapper.selectList();
    }

    @Override
    public List<IotInspectOrderDetailDO> getListStat(IotCountDataDO vo, Set<Long> ids) {
        return iotInspectOrderDetailMapper.selectListStat(vo, ids);
    }
    @Override
    public Long selectExceptionReport(IotCountDataDO vo, Set<Long> ids) {
        return iotInspectOrderDetailMapper.selectExceptionReport(vo, ids);
    }
    @Override
    public PageResult<IotInspectDeviceVo> getDeviceStatus(IotInspectOrderDetailPageReqVO pageReqVO) {
        IPage<IotInspectDeviceVo> page;
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        if ("todo".equals(pageReqVO.getStatus())) {
            page = iotInspectOrderDetailMapper.getDeviceStatusWei(
                    new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO, ids);
        } else if ("finished".equals(pageReqVO.getStatus())) {
            page = iotInspectOrderDetailMapper.getDeviceStatusYi(
                    new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO, ids);
        } else {
            page = iotInspectOrderDetailMapper.getDeviceStatusIgnore(
                    new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO, ids);
        }

        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    public PageResult<IotInspectOrderDetailDO> getOrderDetailItem(IotInspectOrderDetailPageReqVO pageReqVO) {
        PageResult<IotInspectOrderDetailDO> need = new PageResult<>();
        Set<Long> ids = new HashSet<>();
        String companyCode;
        if (Objects.isNull(pageReqVO.getDeptId())){
            Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
            companyCode = deptUtil.getCompanyCode(loginUserDeptId);
            ids = deptUtil.getDeptIds(companyCode);
        } else {
            companyCode = deptUtil.getCompanyCode(pageReqVO.getDeptId());
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        Set<Long> deviceIds = new HashSet<>();
        if (StringUtils.isNotBlank(pageReqVO.getDeviceCode())) {
            IotDevicePageReqVO devicePageReqVO = new IotDevicePageReqVO();
            devicePageReqVO.setDeviceCode(pageReqVO.getDeviceCode());
            deviceIds = iotDeviceMapper.selectList(devicePageReqVO).stream().map(IotDeviceDO::getId).collect(Collectors.toSet());
        }
        Set<Long> orderIds = new HashSet<>();
        if (StringUtils.isNotBlank(pageReqVO.getOrderName())) {
            IotInspectOrderPageReqVO iotInspectOrderPageReqVO = new IotInspectOrderPageReqVO();
            iotInspectOrderPageReqVO.setInspectOrderTitle(pageReqVO.getOrderName());
            orderIds = iotInspectOrderMapper.selectList(iotInspectOrderPageReqVO).stream().map(IotInspectOrderDO::getId).collect(Collectors.toSet());
        }
        if ("待填写".equals(pageReqVO.getStatus())){
//            pageReqVO.setIfNormal(null);
            need = iotInspectOrderDetailMapper.selectItemPageNull(pageReqVO, ids, deviceIds, orderIds);
        } else if ("正常".equals(pageReqVO.getStatus())){
            pageReqVO.setIfNormal(true);
            need = iotInspectOrderDetailMapper.selectItemPage(pageReqVO, ids, deviceIds, orderIds);
        } else if ("异常".equals(pageReqVO.getStatus())){
            pageReqVO.setIfNormal(false);
            need = iotInspectOrderDetailMapper.selectItemPage(pageReqVO, ids, deviceIds, orderIds);
        }
        return need;
    }
}
