package com.pqs9000.controller.point;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.pointInfo.LineLedger;
import com.njcn.pojo.pointInfo.PointInfo;
import com.njcn.pojo.terminal.DeviceUnitPO;
import com.njcn.pojo.terminal.DeviceUnitVO;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.service.point.PointBaseInfoService;
import com.njcn.utils.PubUtils;
import com.njcn.pojo.pointInfo.LineLedgerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: Sunwei 【sunW2016@163.com】
 * @Description:
 * @Date: Create in 13:22 2018/5/28
 * @Modified By:
 */
@RestController
public class PointBaseInfoController {
    @Autowired
    private PointBaseInfoService pointBaseInfoService;

    @Autowired
    UserLogDetailService userLog;

    @PostMapping(value = "/getPointInfo")
    public HttpResult getPointInfo(Integer pointIndex, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        PointInfo pointInfo;

        try {
            pointInfo = pointBaseInfoService.getPointInfo(pointIndex);
            userLog.getLog("查询监测点详情", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("监测点详情获取失败.");
            userLog.getLog("查询监测点详情", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        if (pointInfo != null) {
            httpResult.setCode(200);
            httpResult.setBody(pointInfo);
            httpResult.setMessage("监测点详情获取成功.");
        } else {
            httpResult.setCode(500);
            httpResult.setMessage("监测点详情获取失败.");
        }

        return httpResult;
    }

    /**
     * 通过监测点获取监测点国标限值
     * @author cdf
     * @date 2021/5/26
     */
    @GetMapping("getOverLimit")
    public HttpResult getOverLimit(Integer pointIndex){
        Map<String, Object> map = pointBaseInfoService.getOverLimit(pointIndex);
        if(map!=null){
            return PubUtils.assignmentResult(map,200,"获取限值成功");
        }else {
            return PubUtils.assignmentResult(null,500,"获取限值失败");
        }
    }

    /**
     * 监测点台账信息查询
     * @author cdf
     * @date 2021/9/14
     */
    @PostMapping("getLineLedger")
    public HttpResult getLineLedger(@RequestBody LineLedgerVO lineLedgerVO){
        List<LineLedger> list = pointBaseInfoService.getLineLedger(lineLedgerVO);

        return PubUtils.assignmentResult(list,200,"监测点台账信息查询成功");
    }


    /**
     * 根据监测点获取监测点的单位信息
     * @author cdf
     * @date 2023/8/17
     */
    @GetMapping("lineUnitDetail")
    public HttpResult lineUnitDetail(@RequestParam("lineId")Integer lineId){
        DeviceUnitVO deviceUnitVO =  pointBaseInfoService.lineUnitDetail(lineId);
        return PubUtils.assignmentResult(deviceUnitVO,200,"获取单位成功");
    }

    /**
     * 根据监测点获取监测点的单位信息
     * @author cdf
     * @date 2023/8/17
     */
    @GetMapping("devUnitDetail")
    public HttpResult devUnitDetail(@RequestParam("devId")Integer devId){
        DeviceUnitPO deviceUnitPO =  pointBaseInfoService.devUnitDetail(devId);
        return PubUtils.assignmentResult(deviceUnitPO,200,"获取单位成功");
    }


}
