package org.dromara.inspection.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.inspection.domain.vo.OpsInspectionItemVo;
import org.dromara.inspection.mapper.OpsInspectionItemMapper;
import org.dromara.personnel.domain.OpsUser;
import org.dromara.personnel.service.impl.OpsUserServiceImpl;
import org.dromara.system.api.RemoteUserService;
import org.dromara.system.api.domain.vo.RemoteUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer;
import org.springframework.stereotype.Service;
import org.dromara.inspection.domain.bo.OpsInspectionPlanBo;
import org.dromara.inspection.domain.vo.OpsInspectionPlanVo;
import org.dromara.inspection.domain.OpsInspectionPlan;
import org.dromara.inspection.mapper.OpsInspectionPlanMapper;
import org.dromara.inspection.service.IOpsInspectionPlanService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 运维-巡检-计划Service业务层处理
 *
 * @author LionLi
 * @date 2025-09-19
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OpsInspectionPlanServiceImpl implements IOpsInspectionPlanService {

    @DubboReference
    private RemoteUserService remoteUserService;

    private final OpsInspectionPlanMapper baseMapper;
    private final OpsInspectionItemMapper itemMapper;

    /**
     * 查询运维-巡检-计划
     *
     * @param id 主键
     * @return 运维-巡检-计划
     */
    @Override
    public OpsInspectionPlanVo queryById(Long id){
        OpsInspectionPlanVo opsInspectionPlanVo = baseMapper.selectVoById(id);
        if (opsInspectionPlanVo == null){
            return null;
        }
        List<OpsInspectionPlanVo> opsInspectionPlanVo1 = List.of(opsInspectionPlanVo);
        setValue(opsInspectionPlanVo1);
        return opsInspectionPlanVo1.getFirst();
    }

    /**
     * 分页查询运维-巡检-计划列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 运维-巡检-计划分页列表
     */
    @Override
    public TableDataInfo<OpsInspectionPlanVo> queryPageList(OpsInspectionPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OpsInspectionPlan> lqw = buildQueryWrapper(bo);
        Page<OpsInspectionPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        setValue(result.getRecords());
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的运维-巡检-计划列表
     *
     * @param bo 查询条件
     * @return 运维-巡检-计划列表
     */
    @Override
    public List<OpsInspectionPlanVo> queryList(OpsInspectionPlanBo bo) {
        LambdaQueryWrapper<OpsInspectionPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OpsInspectionPlan> buildQueryWrapper(OpsInspectionPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OpsInspectionPlan> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OpsInspectionPlan::getId);
        lqw.eq(bo.getProjectId() != null, OpsInspectionPlan::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanType()), OpsInspectionPlan::getPlanType, bo.getPlanType());
        lqw.eq(StringUtils.isNotBlank(bo.getObjectType()), OpsInspectionPlan::getObjectType, bo.getObjectType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), OpsInspectionPlan::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增运维-巡检-计划
     *
     * @param bo 运维-巡检-计划
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OpsInspectionPlanBo bo) {
        OpsInspectionPlan add = MapstructUtils.convert(bo, OpsInspectionPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运维-巡检-计划
     *
     * @param bo 运维-巡检-计划
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OpsInspectionPlanBo bo) {
        OpsInspectionPlan update = MapstructUtils.convert(bo, OpsInspectionPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OpsInspectionPlan entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除运维-巡检-计划信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 回显信息赋值
     */
    private void setValue(List<OpsInspectionPlanVo> list){
        list.forEach(record -> {
            List<RemoteUserVo> remoteUserVos = remoteUserService.selectListByIds(List.of(record.getPerson()));

            if (remoteUserVos != null && !remoteUserVos.isEmpty()){
                record.setNickName(remoteUserVos.getFirst().getUserName());
            }else{
                record.setNickName("未知用户");
            }

            if (record.getInspectionItemId()!=null){
                String[] itemIds = record.getInspectionItemId().split(",");
                List<OpsInspectionItemVo> itemVos = new ArrayList<>();
                for (String itemId : itemIds) {
                    OpsInspectionItemVo itemVo = itemMapper.selectVoById(itemId);
                    if (itemVo != null){
                        itemVos.add(itemVo);
                    }
                }
                record.setItemVoList(itemVos);
            }

        });
    }

}
