package com.sckw.path.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.path.entity.KwBusinessPath;
import com.sckw.path.entity.req.AddPathParam;
import com.sckw.path.entity.req.LinePathFindListParam;
import com.sckw.path.entity.req.SelectPathParam;
import com.sckw.path.entity.req.UpdatePathParam;
import com.sckw.path.entity.res.PathDetail;
import com.sckw.path.entity.res.PathFindListRes;
import com.sckw.path.entity.res.PathPageListRes;
import com.sckw.path.mapper.KwBusinessPathMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.*;
import com.sckw.core.model.page.PageRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-04-16
 */
@Service
public class KwPathService {

    @Autowired
    KwBusinessPathMapper kwBusinessPathMapper;

    public void add(AddPathParam addPathParam)
    {
        //路径名称存在.不能重复
        LambdaQueryWrapper<KwBusinessPath> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KwBusinessPath::getName, addPathParam.getName())
                .eq(KwBusinessPath::getDelFlag, Global.NO);
        KwBusinessPath kwBusinessPath = kwBusinessPathMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotNull(kwBusinessPath)) {
            throw new RuntimeException("路径名称已存在,不能重复");
        }
        String loadName = addPathParam.getLoadName();
        String unLoadName = addPathParam.getUnloadName();
        if (Objects.equals(loadName, unLoadName)) {
            throw new RuntimeException("路径起始和节数位置不能相同");
        }
        //插入数据
        KwBusinessPath kwBusinessPathInsert = BeanUtils.copyProperties(addPathParam, KwBusinessPath.class);
        kwBusinessPathMapper.insert(kwBusinessPathInsert);


    }


    /**
    * @Description: 路径详情
    * @Author: Lt
    * @Date: 2024/4/16 0016 19:19  
    */
    public PathDetail detail(Long id) {
        LambdaQueryWrapper<KwBusinessPath> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KwBusinessPath::getId, id)
                .eq(KwBusinessPath::getDelFlag, Global.NO)
                .last("LIMIT 1");
        KwBusinessPath freightRate = kwBusinessPathMapper.selectOne(queryWrapper);
        if (Objects.isNull(freightRate)) {
            throw new RuntimeException("路径信息不存在");
        }
        return BeanUtil.copyProperties(freightRate, PathDetail.class);
    }

    /**
    * @Description: 路径编辑
    * @Author: Lt
    * @Date: 2024/4/16 19:55
    */
    public void update(UpdatePathParam param) {
        KwBusinessPath lineFreightRate = kwBusinessPathMapper.selectById(param.getId());
        if (Objects.isNull(lineFreightRate)) {
            throw new RuntimeException("路径信息不存在！");
        }
        Long count = countByEntIdAndName(param.getName(), param.getId());
        if (count > 0) {
            throw new RuntimeException("路径名称已存在，不可重复！");
        }
        KwBusinessPath freightRate = BeanUtil.copyProperties(param, KwBusinessPath.class);
        String loadName = param.getLoadName();
        String unloadName = param.getUnloadName();
        if (Objects.equals(loadName, unloadName)) {
            throw new RuntimeException("路径起始结束位置不能相同！");
        }
        kwBusinessPathMapper.updateById(freightRate);
    }

    public Long countByEntIdAndName(String name, Long id) {
        LambdaQueryWrapper<KwBusinessPath> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Objects.nonNull(id), KwBusinessPath::getId, id)
                .eq(KwBusinessPath::getName, name)
                .eq(KwBusinessPath::getDelFlag, Global.NO);
        return kwBusinessPathMapper.selectCount(queryWrapper);
    }

    /**
    * @Description: 分页查询
    * @Author: Lt
    * @Date: 2024/4/16 0016 20:12  
    */
    public PageRes<PathPageListRes> select(SelectPathParam params)
    {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<PathPageListRes> kwBusinessPaths = kwBusinessPathMapper.selectPageList(params);
        return new PageRes<>(new PageInfo<>(kwBusinessPaths));
    }

    /**
    * @Description: 路径下拉列表查询
    * @Author: Lt
    * @Date: 2024/4/16 0016 20:15  
    */
    public PageRes<PathFindListRes> findList(LinePathFindListParam kwBusinessGoods) {
        List<PathFindListRes> resultList;

        // 判断是否需要分页
        if (kwBusinessGoods.getPage() != null && kwBusinessGoods.getPageSize() != null) {
            // 分页查询
            PageHelper.startPage(kwBusinessGoods.getPage(), kwBusinessGoods.getPageSize());
            resultList = kwBusinessPathMapper.selectFindList(kwBusinessGoods);
            return new PageRes<>(new PageInfo<>(resultList)); // 注意这里应该返回resultList而不是kwBusinessGoods
        } else {
            // 不分页查询
            resultList = kwBusinessPathMapper.selectFindList(kwBusinessGoods);
            // 直接返回结果列表，不包装PageInfo，除非你有其他非分页的PageRes构造方式
            return new PageRes<>(resultList); // 假设PageRes有合适的构造方法处理非分页情况
        }
    }


    public void batchDelete(List<Long> ids) {
        //todo 关联托运、承运订单不可删除
        LambdaQueryWrapper<KwBusinessPath> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwBusinessPath::getId, ids)
                .eq(KwBusinessPath::getDelFlag, Global.NO);
        List<KwBusinessPath> list = kwBusinessPathMapper.selectList(wrapper);
        if (!Objects.equals(ids.size(), list.size())) {
            throw new RuntimeException("删除数据与数据库数据不一致");
        }
        LambdaUpdateWrapper<KwBusinessPath> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(KwBusinessPath::getDelFlag, Global.YES).in(KwBusinessPath::getId, ids);
        kwBusinessPathMapper.update(null, updateWrapper);
    }


}
