package cn.com.ty.lift.system.version.controller;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.system.version.dao.entity.Version;
import cn.com.ty.lift.system.version.dao.mapper.VersionMapper;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 软件版本信息
 */
@RestController
@AllArgsConstructor
public class VersionController {
    VersionMapper versionMapper;

    /**
     * 获取软件版本信息
     *
     * @param request 获取请求头信息
     * @return 版本信息
     */
    @PostMapping("version")
    public RestResponse<?> version(HttpServletRequest request, @RequestBody Version version) {
        String mobileType = request.getHeader("mobileType");
        if (mobileType == null || version.getDeviceModel() == null) {
            return RestResponse.fail();
        }
        int type = Integer.parseInt(mobileType);
        QueryWrapper<Version> query = Wrappers.query();
        switch (type) {
            case ApiConstants.ACCESS_TYPE_PC:
                query.eq("device", ApiConstants.ACCESS_TYPE_PC).eq("device_model", version.getDeviceModel());
                break;
            case ApiConstants.ACCESS_TYPE_APP:
                query.eq("device", ApiConstants.ACCESS_TYPE_APP).eq("device_model", version.getDeviceModel());
                break;
            case ApiConstants.ACCESS_TYPE_PUBLIC:
                query.eq("device", ApiConstants.ACCESS_TYPE_PUBLIC).eq("device_model", version.getDeviceModel());
                break;
            case ApiConstants.ACCESS_TYPE_APPLETS:
                query.eq("device", ApiConstants.ACCESS_TYPE_APPLETS).eq("device_model", version.getDeviceModel());
                break;
        }
        return RestResponse.success(versionMapper.selectOne(query));
    }
}
