package com.tuoheng.admin.service.impl;


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.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.Section;

import com.tuoheng.admin.entity.SectionDept;
import com.tuoheng.admin.mapper.RoadInformationMapper;
import com.tuoheng.admin.mapper.SectionDeptMapper;
import com.tuoheng.admin.mapper.SectionMapper;
import com.tuoheng.admin.query.SectionQuery;
import com.tuoheng.admin.service.ISectionService;
import com.tuoheng.admin.service.section.query.QuerySectionListByDeptIdService;
import com.tuoheng.admin.service.section.query.QuerySectionListByRoadIdService;
import com.tuoheng.admin.vo.SectionInfoVo;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * @Author ChengWang
 * @Date 2022/11/17
 */
@Service
public class SectionServiceImpl extends BaseServiceImpl<SectionMapper, Section> implements ISectionService {

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private RoadInformationMapper roadInformationMapper;

    @Autowired
    private SectionDeptMapper sectionDeptMapper;

    @Autowired
    private QuerySectionListByDeptIdService querySectionListByDeptIdService;

    @Autowired
    private QuerySectionListByRoadIdService querySectionListByRoadIdService;

    @Override
    public JsonResult queryPage(SectionQuery query) {
        if(null==query.getLimit() && null == query.getPage()){
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        //获取分页数据
        IPage<Section> page = new Page<>(query.getPage(),query.getLimit());
        IPage<Section> pageData = sectionMapper.selectPage(page, Wrappers.<Section>lambdaQuery()
                .eq(Section::getMark, 1)
                .eq(Section::getTenantId, 0)
                .orderByDesc(Section::getCreateTime));
        List<Section> records = pageData.getRecords();
        List<Section> list = new ArrayList<>();
        for (Section record : records) {
            //获取每条路段对应的公路id
            RoadInformation roadInformation = roadInformationMapper.selectOne(Wrappers.<RoadInformation>lambdaQuery()
                    .eq(RoadInformation::getId, record.getRoadId())
                    .eq(RoadInformation::getMark, 1));
            if(StringUtils.isNotNull(roadInformation)){
                if(StringUtils.isNotEmpty(roadInformation.getCode())){
                    record.setCode(roadInformation.getCode());
                }
            }
            //判断路段是否关联部门
            SectionRelation(record);
            list.add(record);
        }
        pageData.setRecords(list);

        return JsonResult.success(pageData);
    }

    @Override
    public JsonResult getListInfo(SectionQuery query) {
        List<Section> sectionList = sectionMapper.selectList(new LambdaQueryWrapper<Section>()
                .eq(Section::getMark, 1)
                .eq(Section::getTenantId, 0)
                .orderByDesc(Section::getCreateTime));
        List<Section> list = new ArrayList<>();
        for (Section section : sectionList) {
            //判断路段是否关联部门
            SectionRelation(section);
            list.add(section);
        }

        return JsonResult.success(list);
    }

    /**
     * 根据部门id获取该部门下路段信息列表
     * @param deptId
     * @return
     */
    @Override
    public JsonResult getListByDeptId(String deptId) {
        return querySectionListByDeptIdService.getList(deptId);
    }

    /**
     * 根据公路id获取该公路下路段信息列表
     * @param roadId
     * @return
     */
    @Override
    public JsonResult getListByRoadId(String roadId) {
        return querySectionListByRoadIdService.getList(roadId);
    }

    @Override
    public JsonResult editInfo(Section entity) {
//        User user = (User) ThreadLocalUtil.get();
//        if (StringUtils.isNull(user)) {
//            return JsonResult.error("获取不到用户信息");
//        }
        if(StringUtils.isEmpty(entity.getId())){
            //新增
            entity.setCreateTime(DateUtils.now());
            entity.setCreateUser("ca");
        }else {
            //更新
            entity.setUpdateTime(DateUtils.now());
            entity.setUpdateUser("ca");
        }
        super.edit(entity);

        return JsonResult.success();
    }

    @Override
    public JsonResult getSectionInfo(String sectionId) {
        if(StringUtils.isEmpty(sectionId)){
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        //根据id查询路段
        Section section = sectionMapper.selectOne(Wrappers.<Section>lambdaQuery()
                .eq(Section::getMark, 1).eq(Section::getId, sectionId));
        String roadId = section.getRoadId();
        if(StringUtils.isEmpty(roadId)){
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        //根据roadId查询对应的公路
        RoadInformation roadInformation = roadInformationMapper.selectOne(new LambdaQueryWrapper<RoadInformation>()
                .eq(RoadInformation::getId, roadId)
                .eq(RoadInformation::getMark, 1));
        //获取公路代号
        if(StringUtils.isNull(roadInformation)){
            return JsonResult.error("对应的公路为空");
        }
        String code = roadInformation.getCode();
        if(StringUtils.isEmpty(code)){
            return JsonResult.error("公路代号为空");
        }
        SectionInfoVo sectionInfoVo = new SectionInfoVo();
        BeanUtils.copyProperties(section,sectionInfoVo);
        sectionInfoVo.setCode(code);

        return JsonResult.success(sectionInfoVo);
    }


    private void SectionRelation(Section section) {
        List<SectionDept> sectionDeptList = sectionDeptMapper.selectList(Wrappers.<SectionDept>lambdaQuery()
                .eq(SectionDept::getTenantId, 0)
                .eq(SectionDept::getSectionId, section.getId()));
        if(StringUtils.isNotEmpty(sectionDeptList)){
            //路段关联与部门关联，设置关联字段为true
            section.setRelation(true);
        }else{
            section.setRelation(false);
        }
    }


}
