package com.xhkjedu.sapi.controller.appversion;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.appversion.TAppthird;
import com.xhkjedu.sapi.model.appversion.TAppthirdHistory;
import com.xhkjedu.sapi.service.appversion.AppthirdService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdDetailVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* @author ywx
* @classname TAppthirdController
* @description
* @date 2020/06/17 10:02
**/
@RestController
@RequestMapping("/appthird")
@Slf4j
public class AppthirdController {
    @Resource
    private AppthirdService appthirdService;

    @PostMapping("/add")
    @Action("第三方应用-增加")
    public ResultVo add(@RequestBody TAppthird appthird) {
        N_Utils.validation(new Object[]{
                appthird.getAppname(),"第三方应用名称",2,
                appthird.getApplogo(),"第三方应用图标",2
        });
        Integer id = appthirdService.save(appthird);
        return new ResultVo(0, "保存成功",id);
    }


    @PostMapping("/update")
    @Action("第三方应用-修改")
    public ResultVo update(@RequestBody TAppthird appthird) {
        N_Utils.validation(new Object[]{appthird.getAppid(),"第三方应用id",1});
        appthirdService.update(appthird);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/delete")
    @Action("第三方应用-删除")
    public ResultVo delete(@RequestBody TAppthird appthird) {
        N_Utils.validation(new Object[]{appthird.getAppid(),"第三方应用id",1});
        appthirdService.deleteById(appthird.getAppid());
        return new ResultVo(0,"删除成功");
    }


    @PostMapping("/update_state")
    @Action("第三方应用-修改状态")
    public ResultVo updateState(@RequestBody TAppthird appthird) {
        Integer appid = appthird.getAppid();
        Integer appstate = appthird.getAppstate();
        N_Utils.validation(new Object[]{appid,"第三方应用id",1,appstate,"应用状态",1});

        try {
            appthirdService.updateState(appid, appstate);
        }catch (Exception e){
            log.error("上架失败：" + e.getMessage());
            return new ResultVo(1,"上架失败：" + e.getMessage());
        }

        return new ResultVo(0,"上、下架成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TAppthird appthird) {
        N_Utils.validation(new Object[]{appthird.getAppid(),"第三方应用id",1});
        AppthirdDetailVo detailVo = appthirdService.findById(appthird.getAppid());
        return new ResultVo(0,"获取详情成功",detailVo);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TAppthird appthird) {
        List<AppthirdVo> list = appthirdService.listAll(appthird.getSchoolid());
        return new ResultVo(0,"获取应用主页列表成功",list);
    }

    @PostMapping("/list_my")
    public ResultVo listMy(@RequestBody TAppthird appthird) {
        N_Utils.validation(new Object[]{appthird.getCreateid(),"用户id",1});
        List<AppthirdVo> list = appthirdService.listMy(appthird.getCreateid());
        return new ResultVo(0,"获取我的应用列表成功",list);
    }


    @PostMapping("/add_history")
    @Action("第三方应用-增加版本")
    public ResultVo addHistory(@RequestBody TAppthirdHistory history) {
        Integer id = appthirdService.saveHistory(history);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/delete_history")
    @Action("第三方应用-删除版本")
    public ResultVo deleteHistory(@RequestBody TAppthirdHistory history) {
        N_Utils.validation(new Object[]{history.getApphid(),"第三方应用历史id",1});
        appthirdService.deleteByApphId(history.getApphid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/get_new")
    public ResultVo getNew(@RequestBody TAppthird appthird) {
        Integer appid = appthird.getAppid();
        N_Utils.validation(new Object[]{appid,"第三方应用id",1});
        TAppthirdHistory appversion = appthirdService.getNew(appid);
        return new ResultVo(0,"获取应用最新版本成功",appversion);
    }

    @PostMapping("/list_new")
    public ResultVo listNew(@RequestBody TAppthird appthird) {
        Integer schoolid = appthird.getSchoolid();
        Integer appstate = appthird.getAppstate();
        List<TAppthirdHistory> list = appthirdService.listNew(schoolid, appstate);
        return new ResultVo(0,"获取最新应用列表成功",list);
    }
}
