package cn.ezhizao.project.business.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.domain.BizApp;
import cn.ezhizao.project.business.service.IBizAppService;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * app版本管理Controller
 * 
 * @author 朱汕
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/business/app")
public class BizAppController extends BaseController
{
    @Resource
    private IBizAppService bizAppService;

    /**
     * 查询app版本管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:app:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizApp bizApp)
    {
        startPage();
        List<BizApp> list = bizAppService.getList(bizApp);
        return getDataTable(list);
    }

    /**
     * 导出app版本管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:app:export')")
    @Log(title = "app版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizApp bizApp)
    {
        List<BizApp> list = bizAppService.getList(bizApp);
        ExcelUtil<BizApp> util = new ExcelUtil<BizApp>(BizApp.class);
        util.exportExcel(response, list, "app版本管理数据");
    }

    /**
     * 获取app版本管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:app:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizAppService.getById(id));
    }

    /**
     * 新增app版本管理
     */
    @PreAuthorize("@ss.hasPermi('business:app:add')")
    @Log(title = "app版本管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizApp bizApp)
    {
        bizApp.setAuthor(SecurityUtils.getLoginUser().getUser().getNickName());
        return toAjax(bizAppService.save(bizApp));
    }

    /**
     * 修改app版本管理
     */
    @PreAuthorize("@ss.hasPermi('business:app:edit')")
    @Log(title = "app版本管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizApp bizApp)
    {
        return toAjax(bizAppService.updateById(bizApp));
    }

    /**
     * 删除app版本管理
     */
    @PreAuthorize("@ss.hasPermi('business:app:remove')")
    @Log(title = "app版本管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizAppService.removeBatchByIds(ids));
    }

    /**
     * 检查更新
     */
    @PostMapping("/getUpdate")
    public AjaxResult getUpdate(@RequestBody BizApp bizApp)
    {
        List<BizApp> bizApps = bizAppService.query()
                .eq("app_id",bizApp.getAppId()).orderByDesc("create_time").list();
        return bizApps.size()>0?success(bizApps.get(0)):error("");
    }
}
