package com.huimv.receive.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.common.utils.UploadImage;
import com.huimv.receive.entity.BillAccessDoor;
import com.huimv.receive.entity.BillGate;
import com.huimv.receive.entity.DeviceList;
import com.huimv.receive.service.IBillAccessDoorService;
import com.huimv.receive.service.IBillGateService;
import com.huimv.receive.service.IDeviceListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * <p>
 * 车辆闸机信息记录 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-25
 */
@RestController
@RequestMapping("/bill-gate")
@CrossOrigin
public class BillGateController {

    @Autowired
    private IBillGateService gateService;
    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private IDeviceListService deviceListService;
    @Autowired
    private IBillAccessDoorService doorService;

    @RequestMapping("/getGateData")
    public Result getDoorData(@RequestParam("time") String time,
                              @RequestParam(value = "plateNumber", required = false) String plateNumber,
                              @RequestParam("result") String result,
                              @RequestParam("image") MultipartFile image,
                              @RequestParam("userName") String userName,
                              @RequestParam("seq") String seq) throws ParseException, IOException {
        BillAccessDoor door = new BillAccessDoor();

        door.setBillStatus(Integer.parseInt(result));
        DateFormat def = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        door.setCheckDate(def.parse(time));

        String imageCom = "";
        if (ObjectUtil.isNotEmpty(image)) {
            if (uploadImage.getImageCom(image).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片上传失败", false);
            }
            imageCom = uploadImage.getImageCom(image);
            door.setUrlImg(imageCom);
        }

        //后面需要修改
        QueryWrapper<DeviceList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_code", seq);
        DeviceList deviceList = deviceListService.getOne(queryWrapper);
        door.setFarmId(Integer.valueOf(deviceList.getFarmId()));
        door.setLocationId(deviceList.getLocationId());
        door.setCheckLocation(deviceList.getLocationName());
        door.setUserName(userName);
        doorService.save(door);
        return new Result(ResultCode.SUCCESS, time, imageCom);
    }

}
