package com.ozs.web.controller.accountmanagment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.entity.BaseVehicle;
import com.ozs.entity.VehiclePosition;
import com.ozs.entity.vo.VehiclePositionResp;
import com.ozs.framework.manager.factory.AsyncFactory;
import com.ozs.service.BaseVehicleService;
import com.ozs.service.VehiclePositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wyy
 * @subject
 * @creat 2023/4/12
 */
@RestController
@RequestMapping("/monitor/vehicleposition")
public class VehiclePositionController extends BaseController {
    @Autowired
    private VehiclePositionService vehiclePositionService;
    @Autowired
    private BaseVehicleService baseVehicleService;


    /**
     * 获取机车行驶日志分页列表
     *
     * @param vehiclePositionResp
     * @return
     */
    @PostMapping("/list")
    @Log(title = "机车行驶日志", businessType = BusinessType.SELECT)
    public AjaxResult list(@RequestBody VehiclePositionResp vehiclePositionResp) {
        Integer pageNum = vehiclePositionResp.getPageNum().intValue();
        Integer pageSize = vehiclePositionResp.getPageSize().intValue();
        List<VehiclePosition> list = vehiclePositionService.selectVehiclePositionList(vehiclePositionResp);
        if(!ObjectUtils.isEmpty(list)){
            list.forEach(l -> {
                if (!ObjectUtils.isEmpty(l.getTravelMile())) {
                    l.setTravelMileFormat(AppendUtils.stringAppend(Integer.valueOf(l.getTravelMile())));
                }
                if(!ObjectUtils.isEmpty(l.getLineDir())){
                    l.setLineDirName(l.getLineDir() == 1 ? "上行" : "下行");
                }
            });
        }
        IPage page = vehiclePositionService.listToPage(list, pageNum, pageSize);
        return AjaxResult.success(page);
    }

    /**
     * 获取机车行驶日志列表
     * @return
     */
    @PostMapping("/listNoPage")
    @Log(title = "机车行驶日志", businessType = BusinessType.SELECT)
    public AjaxResult listNoPage() {
        List<BaseVehicle> list = baseVehicleService.list();
        return AjaxResult.success(list);
    }

    /**
     * 车载终端监控列表
     * @return
     */
    @PostMapping("/vehiclePositionPage")
    @Log(title = "车载终端监控列表", businessType = BusinessType.SELECT)
    public AjaxResult vehiclePositionPage(@RequestBody VehiclePositionResp vehiclePositionResp) {
        Integer pageNum = vehiclePositionResp.getPageNum().intValue();
        Integer pageSize = vehiclePositionResp.getPageSize().intValue();
        List<VehiclePosition> list = vehiclePositionService.vehiclePositionPage(vehiclePositionResp);
        IPage page = vehiclePositionService.listToPage(list, pageNum, pageSize);
        return AjaxResult.success(page);
    }

    @Log(title = "机车行驶日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody VehiclePositionResp vehiclePositionResp) {
        List<VehiclePosition> list = vehiclePositionService.selectVehiclePositionList(vehiclePositionResp);
        if(!ObjectUtils.isEmpty(list)){
            list.forEach(l -> {
                if (!ObjectUtils.isEmpty(l.getTravelMile())) {
                    l.setTravelMileFormat(AppendUtils.stringAppend(Integer.valueOf(l.getTravelMile())));
                }
                if(!ObjectUtils.isEmpty(l.getLineDir())){
                    l.setLineDirName(l.getLineDir() == 1 ? "上行" : "下行");
                }
            });
        }
        ExcelUtil<VehiclePosition> util = new ExcelUtil<VehiclePosition>(VehiclePosition.class);
        util.exportExcel(response, list, "机车行驶日志");
    }
}
