package com.huimv.receive.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.receive.common.token.TokenSign;
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.entity.vo.DoorVo;
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.*;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

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

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

    @RequestMapping("/listById")
    public Result listById(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");//1为人脸门禁 2为车辆闸机
        String id = paramsMap.get("id");
        Integer id1 = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if ("1".equals(type)) {
            QueryWrapper<BillAccessDoor> queryWrapper = new QueryWrapper();
            if (277==id1) {
                queryWrapper.eq("id", id);
            } else {
                queryWrapper.eq("id", id).in("farm_id", farmId, 1, 2);
            }
            BillAccessDoor doorServiceOne = doorService.getOne(queryWrapper);
            return new Result(ResultCode.SUCCESS, doorServiceOne);
        } else {
            QueryWrapper<BillGate> queryWrapper = new QueryWrapper();
            if (277==id1) {
                queryWrapper.eq("id", id);
            } else {
                queryWrapper.eq("id", id).in("farm_id", farmId, 1, 2);
            }
            BillGate gate = gateService.getOne(queryWrapper);
            return new Result(ResultCode.SUCCESS, gate);
        }

    }

    @RequestMapping("/getDoorData")
    public Result getDoorData(@RequestParam("time") String time,
                              @RequestParam("result") String result,
                              @RequestParam("userId") String userId,
                              @RequestParam("image") MultipartFile image,
                              @RequestParam("userName") String userName,
                              @RequestParam("seq") String seq) throws ParseException, IOException {
        BillGate gate = new BillGate();
        gate.setBillStatus(Integer.parseInt(result));
        DateFormat def = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        gate.setCheckDate(def.parse(time));
        gate.setUserId(userId);
        String imageCom = "";
        if (ObjectUtil.isNotEmpty(image)) {
            if (uploadImage.getImageCom(image).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片上传失败", false);
            }
            imageCom = uploadImage.getImageCom(image);
            gate.setCarUrl(imageCom);
        }//9G0487EPAJD7469

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