package com.huimv.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.DisplayType;
import com.huimv.admin.entity.ProdAnalysis;
import com.huimv.admin.mapper.DisplayTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
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.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2023-11-20
 */
@CrossOrigin
@RestController
@RequestMapping("/display-type")
public class DisplayTypeController {
    @Autowired
    private DisplayTypeMapper displayTypeMapper;

    @RequestMapping("/get")
    public Result get(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String eventType = paramsMap.get("eventType");
        QueryWrapper<DisplayType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("farm_id",farmId).eq("event_type",eventType);
        DisplayType displayType = displayTypeMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(displayType)){
            displayType = new DisplayType();
            displayType.setFarmId(Integer.valueOf(farmId));
            displayType.setEventType(Integer.valueOf(eventType));
            displayType.setDisplayType(0);
            displayTypeMapper.insert(displayType);
        }
        return new Result(ResultCode.SUCCESS,displayType);
    }


    @RequestMapping("/edit")
    public Result edit(HttpServletRequest httpServletRequest, @RequestBody DisplayType displayType) {
        displayTypeMapper.update(displayType,new QueryWrapper<DisplayType>()
                .eq("farm_id",displayType.getFarmId())
                .eq("event_type",displayType.getEventType()));
        return Result.SUCCESS();
    }

}
