package com.unis.crk.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.common.entity.AppVersion;
import com.unis.crk.common.entity.vo.AppVersionVo;
import com.unis.crk.common.service.IAppVersionService;
import com.unis.crk.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 * app版本 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-06-24
 */
@CrossOrigin
@Api(value = "app版本",tags = {"app版本"})
@RestController
@RequestMapping("appVersion")
public class AppVersionController {

    private final static Logger LOGGER = LoggerFactory.getLogger(AppVersionController.class);


    @Autowired
    private IAppVersionService appVersionService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<AppVersion> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.appVersionService.getById(id));
        return data;
    }
    /**
     * 根据ID查询数据
     * @param appCode
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOneByCode/{appCode}")
    public ResultUtil getOneByCode(@PathVariable("appCode") String appCode){
        LOGGER.info("入参为:{}",appCode);
        ResultUtil<AppVersion> data = new ResultUtil<>();
        if (StringUtils.isEmpty(appCode)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.appVersionService.getByCode(appCode));
        return data;
    }

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody AppVersionVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.appVersionService.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param appVersion
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody AppVersion appVersion){
        LOGGER.info("入参为:{}", appVersion);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(appVersion)){
            data.failed("所接收对象为null");
            return data;
        }
        boolean save = this.appVersionService.save(appVersion);
        data.success(save);
        return data;
    }


    /**
     * 修改一条数据
     * @param appVersion
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  AppVersion appVersion){
        LOGGER.info("入参为:{}", appVersion);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(appVersion)){
        data.failed("所接收对象为null");
        return data;
        }
        boolean save = this.appVersionService.updateById(appVersion);
        data.success(save);
        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        boolean save = this.appVersionService.removeById(id);
        data.success(save);
        return data;
    }


}

