package com.yuchen.saas.park.service.service.fire.inspect.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.fire.inspect.FireInspectConfigReqDTO;
import com.yuchen.saas.park.api.dto.fire.inspect.FireInspectConfigSubmitReqDTO;
import com.yuchen.saas.park.api.entity.fire.inspect.SaasParkFireInspectConfig;
import com.yuchen.saas.park.api.vo.fire.inspect.FireInspectConfigVO;
import com.yuchen.saas.park.service.mapper.fire.inspect.SaasParkFireInspectConfigMapper;
import com.yuchen.saas.park.service.service.fire.inspect.ISaasParkFireInspectConfigService;
import com.yuchen.saas.park.service.util.PageConverterUtils;
import lombok.RequiredArgsConstructor;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseEntity;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouhanwei
 * @since 2024-04-01
 */
@Service
@RequiredArgsConstructor
public class SaasParkFireInspectConfigServiceImpl extends BaseServiceImpl<SaasParkFireInspectConfigMapper, SaasParkFireInspectConfig> implements ISaasParkFireInspectConfigService {

    @Override
    public NestPage<FireInspectConfigVO> findFireInspectConfigPage(FireInspectConfigReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        Long customerId = contextUser.getCustomerManageId();
        Page<FireInspectConfigVO> resultPage = new Page<>();
        // 赋值数据
        LambdaQueryWrapper<SaasParkFireInspectConfig> lambdaWrapper = Wrappers.lambdaQuery();
        lambdaWrapper.eq(SaasParkFireInspectConfig::getCustomerId, customerId);
        lambdaWrapper.eq(Objects.nonNull(reqDTO.getProjectId()), SaasParkFireInspectConfig::getProjectId, reqDTO.getProjectId());
        lambdaWrapper.eq(StrUtil.isNotBlank(reqDTO.getInspectConfigType()), SaasParkFireInspectConfig::getInspectConfigType, reqDTO.getInspectConfigType());
        lambdaWrapper.like(StrUtil.isNotBlank(reqDTO.getInspectConfigName()), SaasParkFireInspectConfig::getInspectConfigName, reqDTO.getInspectConfigName());
        lambdaWrapper.orderByDesc(BaseEntity::getCreateTime);
        IPage<SaasParkFireInspectConfig> page = page(Condition.getPage(reqDTO),lambdaWrapper);
        List<SaasParkFireInspectConfig> fireInspectConfigList = page.getRecords();

        // 数据列表
        List<FireInspectConfigVO> resultList = new ArrayList<>();
        for (SaasParkFireInspectConfig saasParkFireInspectConfig : fireInspectConfigList) {
            FireInspectConfigVO vo = new FireInspectConfigVO();
            BeanUtil.copyProperties(saasParkFireInspectConfig, vo);
            resultList.add(vo);
        }
        resultPage.setTotal(page.getTotal());
        resultPage.setSize(page.getSize());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setRecords(resultList);
        return PageConverterUtils.convert(resultPage);
    }

    @Override
    public FireInspectConfigVO getFireInspectConfigById(Long inspectConfigId) {
        FireInspectConfigVO vo = new FireInspectConfigVO();
        SaasParkFireInspectConfig saasParkFireInspectConfig = getById(inspectConfigId);
        if (Objects.isNull(saasParkFireInspectConfig)) {
            throw new ServiceException("巡检点配置详情数据不存在");
        }
        BeanUtil.copyProperties(saasParkFireInspectConfig, vo);
        return vo;
    }

    @Override
    public Boolean addFireInspectConfig(FireInspectConfigSubmitReqDTO reqDTO) {
        SaasParkFireInspectConfig saasParkFireInspectConfig = new SaasParkFireInspectConfig();
        BeanUtil.copyProperties(reqDTO, saasParkFireInspectConfig);
        // 判断去重
        saasParkFireInspectConfig.setCustomerId(AuthUtil.getUser().getCustomerManageId());
        saasParkFireInspectConfig.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
        validRepeat(saasParkFireInspectConfig);
        return save(saasParkFireInspectConfig);
    }

    @Override
    public Boolean editFireInspectConfig(FireInspectConfigSubmitReqDTO reqDTO) {
        SaasParkFireInspectConfig saasParkFireInspectConfig = getById(reqDTO.getId());
        if (Objects.isNull(saasParkFireInspectConfig)) {
            throw new ServiceException("巡检点配置详情数据不存在");
        }
        BeanUtil.copyProperties(reqDTO, saasParkFireInspectConfig);
        // 判断去重
        validRepeat(saasParkFireInspectConfig);
        saasParkFireInspectConfig.setCustomerId(AuthUtil.getUser().getCustomerManageId());
        saasParkFireInspectConfig.setUpdateOrgUser(AuthUtil.getUser().getOrgUserId());
        return updateById(saasParkFireInspectConfig);
    }

    @Override
    public Boolean removeFireInspectConfig(Long inspectConfigId) {
        SaasParkFireInspectConfig saasParkFireInspectConfig = getById(inspectConfigId);
        if (Objects.isNull(saasParkFireInspectConfig)) {
            throw new ServiceException("巡检点配置详情数据不存在");
        }
        return removeById(inspectConfigId);
    }

//    private void setLineLocationInfo(FireInspectConfigVO vo, List<SaasParkFireInspectLocation> locationList) {
//        if (vo.getInspectConfigType().equals(InspectConfigTypeEnum.LOCATION.getCode())) {
//            String[] locations = vo.getInspectConfigTarget().split(",");
//            List<Long> locationIdList = Arrays.stream(locations).map(Long::parseLong).collect(Collectors.toList());
//            List<String> inspectLocationNames = new ArrayList<>();
//            for (SaasParkFireInspectLocation saasParkFireInspectLocation : locationList) {
//                if (locationIdList.contains(saasParkFireInspectLocation.getId())) {
//                    inspectLocationNames.add(saasParkFireInspectLocation.getInspectLocationName());
//                }
//            }
//            vo.setInspectConfigTargetName(CollUtil.join(inspectLocationNames, "->"));
//        }
//    }

    private void validRepeat(SaasParkFireInspectConfig saasParkFireInspectConfig) {
        int count = count(new LambdaQueryWrapper<SaasParkFireInspectConfig>()
                .eq(SaasParkFireInspectConfig::getInspectConfigName, saasParkFireInspectConfig.getInspectConfigName())
                .eq(SaasParkFireInspectConfig::getCustomerId, saasParkFireInspectConfig.getCustomerId())
                .notIn(Objects.nonNull(saasParkFireInspectConfig.getId()), BaseEntity::getId, saasParkFireInspectConfig.getId()));
        if (count > 0) {
            throw new ServiceException("巡检内容标题不能重复");
        }
    }
}
