package vip.xiaonuo.write.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.write.entity.WriteTravelLog;
import vip.xiaonuo.write.entity.WriteTravelRoute;
import vip.xiaonuo.write.entity.vo.WriteTravelPageVo;
import vip.xiaonuo.write.mapper.WriteTravelLogMapper;
import vip.xiaonuo.write.mapper.WriteTravelRouteMapper;
import vip.xiaonuo.write.param.WriteTravelAddParam;
import vip.xiaonuo.write.param.WriteTravelPageParam;
import vip.xiaonuo.write.service.IWriteTravelLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 出差日志 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-25
 */
@Service
public class WriteTravelLogServiceImpl extends ServiceImpl<WriteTravelLogMapper, WriteTravelLog> implements IWriteTravelLogService {

    @Autowired
    private WriteTravelLogMapper logMapper;
    @Autowired
    private WriteTravelRouteMapper routeMapper;

    @Override
    @Transactional
    public CommonResult add(WriteTravelAddParam addParam) {
        WriteTravelLog travelLog = addParam.getTravelLog();
        List<WriteTravelRoute> travelRoutes = addParam.getTravelRoutes();
        Long count = logMapper.selectCount(new QueryWrapper<WriteTravelLog>().eq("order", travelLog.getOrder()));
        if (null == count || count == 0) {
            logMapper.insert(travelLog);
            WriteTravelLog writeTravelLog = logMapper.selectOne(new QueryWrapper<WriteTravelLog>().eq("order", travelLog.getOrder()));
            for (WriteTravelRoute travelRoute : travelRoutes) {
                travelRoute.setLogId(writeTravelLog.getId());
                routeMapper.insert(travelRoute);
            }
            return CommonResult.ok();
        } else {
            return CommonResult.error("编号已存在！");
        }
    }

    @Override
    public List<WriteTravelPageVo> list(WriteTravelPageParam pageParam) {
        String orgId = pageParam.getOrgId();
        QueryWrapper<WriteTravelLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        List<WriteTravelLog> writeTravelLogs = logMapper.selectList(queryWrapper);
        List<WriteTravelPageVo> list = new ArrayList<>();
        if (writeTravelLogs.size() != 0) {
            for (WriteTravelLog writeTravelLog : writeTravelLogs) {
                WriteTravelPageVo vo = new WriteTravelPageVo();
                vo.setWriteTravelLog(writeTravelLog);
                List<WriteTravelRoute> routes = routeMapper.selectList(new QueryWrapper<WriteTravelRoute>().eq("log_id", writeTravelLog.getId()));
                vo.setWriteTravelRoutes(routes);
                list.add(vo);
            }
        }
        return list;
    }
}
