package com.erp.service;
import com.erp.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Slf4j
public class VehicleService extends BaseService {
    @Autowired private CommonService commonService;

    public Map<String, Object> getVehicleList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        commonService.applyPagination(params);
        Number cntNum = sqlSession.selectOne("vehicle.getVehicleListCnt", params);
        int totalCount = cntNum != null ? cntNum.intValue() : 0;
        List<Map<String, Object>> list = sqlSession.selectList("vehicle.getVehicleList", params);
        return commonService.buildListResponse(list, totalCount, params);
    }

    public Map<String, Object> getVehicleInfo(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        return sqlSession.selectOne("vehicle.getVehicleInfo", params);
    }

    @Transactional
    public Map<String, Object> insertVehicle(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.insert("vehicle.insertVehicle", params);
        return params;
    }

    @Transactional
    public Map<String, Object> updateVehicle(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.update("vehicle.updateVehicle", params);
        return params;
    }

    @Transactional
    public Map<String, Object> deleteVehicle(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        sqlSession.delete("vehicle.deleteVehicle", params);
        return params;
    }

    public Map<String, Object> getVehicleLocationList(Map<String, Object> params) {
        List<Map<String, Object>> data = sqlSession.selectList("vehicle.getVehicleLocationList", params);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("data", data);
        return result;
    }

    public Map<String, Object> moveVehicles(Map<String, Object> params) {
        sqlSession.selectList("vehicle.moveVehicles", params);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("message", "차량 위치가 업데이트되었습니다");
        return result;
    }
}
