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.personnel.domain.OpsUser;
import org.dromara.personnel.domain.bo.OpsUserBo;
import org.dromara.personnel.domain.vo.OpsUserVo;
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.stereotype.Service;
import org.dromara.inspection.domain.bo.OpsInspectionTestPlanBo;
import org.dromara.inspection.domain.vo.OpsInspectionTestPlanVo;
import org.dromara.inspection.domain.OpsInspectionTestPlan;
import org.dromara.inspection.mapper.OpsInspectionTestPlanMapper;
import org.dromara.inspection.service.IOpsInspectionTestPlanService;

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

/**
 * 运维-巡检-试验计划Service业务层处理
 *
 * @author LionLi
 * @date 2025-09-20
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OpsInspectionTestPlanServiceImpl implements IOpsInspectionTestPlanService {

    @DubboReference
    private RemoteUserService userService;

    private final OpsInspectionTestPlanMapper baseMapper;

    /**
     * 查询运维-巡检-试验计划
     *
     * @param id 主键
     * @return 运维-巡检-试验计划
     */
    @Override
    public OpsInspectionTestPlanVo queryById(Long id){
        OpsInspectionTestPlanVo opsInspectionTestPlanVo = baseMapper.selectVoById(id);
        List<OpsInspectionTestPlanVo> opsInspectionTestPlanVo1 = List.of(opsInspectionTestPlanVo);
        setValue(opsInspectionTestPlanVo1);
        return opsInspectionTestPlanVo1.getFirst();
    }

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

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

    private LambdaQueryWrapper<OpsInspectionTestPlan> buildQueryWrapper(OpsInspectionTestPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OpsInspectionTestPlan> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(OpsInspectionTestPlan::getId);
        lqw.eq(bo.getProjectId() != null, OpsInspectionTestPlan::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), OpsInspectionTestPlan::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlanCode()), OpsInspectionTestPlan::getPlanCode, bo.getPlanCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTestObject()), OpsInspectionTestPlan::getTestObject, bo.getTestObject());
        lqw.eq(bo.getBeginTime() != null, OpsInspectionTestPlan::getBeginTime, bo.getBeginTime());
        lqw.eq(bo.getEndTime() != null, OpsInspectionTestPlan::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTestInfo()), OpsInspectionTestPlan::getTestInfo, bo.getTestInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getTestSetting()), OpsInspectionTestPlan::getTestSetting, bo.getTestSetting());
        lqw.eq(bo.getPersonCharge() != null, OpsInspectionTestPlan::getPersonCharge, bo.getPersonCharge());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonIds()), OpsInspectionTestPlan::getPersonIds, bo.getPersonIds());
        lqw.eq(StringUtils.isNotBlank(bo.getInspectionItems()), OpsInspectionTestPlan::getInspectionItems, bo.getInspectionItems());
        lqw.eq(StringUtils.isNotBlank(bo.getTestStep()), OpsInspectionTestPlan::getTestStep, bo.getTestStep());
        lqw.eq(StringUtils.isNotBlank(bo.getTestDevice()), OpsInspectionTestPlan::getTestDevice, bo.getTestDevice());
        lqw.eq(StringUtils.isNotBlank(bo.getTestSolutions()), OpsInspectionTestPlan::getTestSolutions, bo.getTestSolutions());
        return lqw;
    }

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

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

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

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

    /**
     * 处理回显数据
     */
    public void setValue(List<OpsInspectionTestPlanVo> list){
        for (OpsInspectionTestPlanVo opsInspectionTestPlanVo : list) {

            if(opsInspectionTestPlanVo.getPersonCharge()!=null) {
                List<RemoteUserVo> byId = userService.selectListByIds(List.of(opsInspectionTestPlanVo.getPersonCharge()));
                if (byId != null && !byId.isEmpty()) {
                    opsInspectionTestPlanVo.setPerson(byId.getFirst());
                }
            }

            if (opsInspectionTestPlanVo.getPersonIds() != null){
                String[] split = opsInspectionTestPlanVo.getPersonIds().split(",");
                List<RemoteUserVo> opsUserVos = new ArrayList<>();
                for (String s : split) {
                    List<RemoteUserVo> opsUserVo = userService.selectListByIds(List.of(Long.parseLong(s)));
                    if (opsUserVo != null && !opsUserVo.isEmpty()){
                        opsUserVos.add(opsUserVo.getFirst());
                    }
                }
                opsInspectionTestPlanVo.setPersons(opsUserVos);
            }

//            if (opsInspectionTestPlanVo.getInspectionItems()!=null){
//                String[] split = opsInspectionTestPlanVo.getInspectionItems().split(",");
//                List<OpsInspectionItemVo> opsInspectionItemVos = new ArrayList<>();
//                for (String s : split) {
//                    OpsInspectionItemVo opsInspectionItemVo = inspectionItemService.queryById(Long.parseLong(s));
//                    if (opsInspectionItemVo != null){
//                        opsInspectionItemVos.add(opsInspectionItemVo);
//                    }
//                }
//            }

        }
    }

}
