package com.xhkjedu.sapi.service.appversion;

import com.xhkjedu.sapi.listener.MessageSender;
import com.xhkjedu.sapi.mapper.appversion.AppthirdHistoryMapper;
import com.xhkjedu.sapi.mapper.appversion.AppthirdManagerMapper;
import com.xhkjedu.sapi.mapper.appversion.AppthirdMapper;
import com.xhkjedu.sapi.model.appversion.TAppthird;
import com.xhkjedu.sapi.model.appversion.TAppthirdHistory;
import com.xhkjedu.sapi.model.appversion.TAppthirdManager;
import com.xhkjedu.sapi.utils.ApiUtil;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.appversion.AppthirdDetailVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author ywx
 * @classname AppthirdService
 * @description 
 * @date 2020/6/17 10:08
 **/
@Service
public class AppthirdService {
    @Resource
    private AppthirdMapper appthirdMapper;
    @Resource
    private AppthirdHistoryMapper appthirdHistoryMapper;
    @Resource
    private AppthirdManagerMapper appthirdManagerMapper;
    @Autowired
    private MessageSender messageSender;

    //添加
    public Integer save(TAppthird appthird) {
        appthird.setAppstate(1);//默认1上架
        appthird.setCreatetime(N_Utils.getSecondTimestamp());
        appthirdMapper.insertUseGeneratedKeys(appthird);//添加应用
        return appthird.getId();
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteById(Integer appid) {
        List<String> resourceUrls = appthirdHistoryMapper.listApppath(appid);//获取APP文件
        handleThirdManager(appid, 2);//处理应用白名单
        Integer result = appthirdMapper.deleteByPrimaryKey(appid);
        if(N_Utils.isListNotEmpty(resourceUrls)){
            messageSender.resourceDel(resourceUrls);//给文件服务器发送mq删除文件
        }
        return result;
    }

    //修改
    public Integer update(TAppthird appthird) {
        return appthirdMapper.updateByPrimaryKeySelective(appthird);
    }

    /**
     * 应用详情
     * @Param [appid]
     * @Author ywx
     * @Date 2020/6/17 10:53
     * @return com.xhkjedu.vo.appversion.AppthirdDetailVo
     **/
    public AppthirdDetailVo findById(Integer appid) {
        return appthirdMapper.findById(appid);
    }

    /**
     * 应用主页列表
     * @Param []
     * @Author ywx
     * @Date 2020/6/17 10:49
     * @return java.util.List<com.xhkjedu.vo.appversion.AppthirdVo>
     **/
    public List <AppthirdVo> listAll(Integer schoolid) {
        return appthirdMapper.listAll(schoolid);
    }

    /**
     * 添加应用历史
     * @Param [history]
     * @Author ywx
     * @Date 2020/6/17 11:06
     * @return java.lang.Integer
     **/
    public Integer saveHistory(TAppthirdHistory history) {
        history.setCreatetime(N_Utils.getSecondTimestamp());
        appthirdHistoryMapper.insertUseGeneratedKeys(history);

        Integer appid = history.getAppid();
        TAppthird appthird = new TAppthird();
        appthird.setAppid(appid);
        appthird.setPackagename(history.getPackagename());
        String apphcomm = history.getApphcomm();
        if (N_Utils.isNotEmpty(apphcomm)) {//应用历史备注不为空，更新应用备注
            appthird.setComm(apphcomm);
        }
        update(appthird);//更新app包名

        handleThirdManager(appid, 1);//处理应用白名单
        return history.getApphid();
    }

    //处理应用白名单
    private void handleThirdManager(Integer appid, Integer appstate) {
        TAppthird app = appthirdMapper.selectByPrimaryKey(appid);
        if (N_Utils.isEmpty(app.getPackagename())) return;//包名为空不处理白名单
        if (appstate.equals(1)) {//上架
            TAppthirdManager manager = new TAppthirdManager();
            manager.setMid(ApiUtil.getId());
            manager.setMname(app.getAppname());
            manager.setMpackage(app.getPackagename());
            manager.setMptype(1);//信任
            manager.setMsystem(2);//不是系统应用
            manager.setMcomm(app.getComm());
            manager.setCreateid(app.getCreateid());
            manager.setCreatetime(N_Utils.getSecondTimestamp());
            manager.setBelong(app.getBelong());
            manager.setSchoolid(app.getSchoolid());
            appthirdManagerMapper.save(manager);
        } else {//下架或删除
            appthirdManagerMapper.deleteByPackageAndSid(app.getPackagename(), app.getSchoolid());
        }
    }

    /**
     * 上、下架
     * @Param [appid, appstate]
     * @Author ywx
     * @Date 2020/6/17 11:19
     * @return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void updateState(Integer appid, Integer appstate) throws Exception{
        //如果是上架，判断是否有apk,20240223因为前端下架后没法上传历史版本
        /*if(appstate == 1){
            //判断apptype类型：1APP 2网站，如果是1的情况下需要获取是否有应用版本
            Integer apptype = appthirdMapper.getAppType(appid);
            if(apptype == 1){
                Integer countHistory = appthirdHistoryMapper.getAppHistoryCount(appid);
                if(!N_Utils.isTrueInteger(countHistory)){
                    throw new Exception("未上传应用版本！");
                }
            }
        }*/

        appthirdMapper.updateState(appid, appstate);
        handleThirdManager(appid, appstate);//处理应用白名单
    }

    /**
     * 删除应用历史
     * @Param [apphid]
     * @Author ywx
     * @Date 2020/6/17 11:45
     * @return void
     **/
    public void deleteByApphId(Integer apphid) {
        appthirdHistoryMapper.deleteByPrimaryKey(apphid);
        List<String> resourceUrls = appthirdHistoryMapper.listApppath2(apphid);//获取APP文件
        if(N_Utils.isListNotEmpty(resourceUrls)){
            messageSender.resourceDel(resourceUrls);//给文件服务器发送mq删除文件
        }
    }

    /**
     * 获取我的应用列表
     * @Param [createid]
     * @Author ywx
     * @Date 2020/6/17 14:24
     * @return java.util.List<com.xhkjedu.vo.appversion.AppthirdVo>
     **/
    public List<AppthirdVo> listMy(Integer createid) {
        return appthirdMapper.listMy(createid);
    }

    /**
     * 获取最新app版本
     * @Param [appid]
     * @Author ywx
     * @Date 2020/6/20 14:40
     * @return com.xhkjedu.model.appversion.TAppthirdHistory
     **/
    public TAppthirdHistory getNew(Integer appid) {
        return appthirdMapper.getNew(appid);
    }

    /**
     * 最新应用列表
     * @Param [schoolid]
     * @Author ywx
     * @Date 2020/6/23 15:40
     * @return java.util.List<com.xhkjedu.model.appversion.TAppthirdHistory>
     **/
    public List<TAppthirdHistory> listNew(Integer schoolid, Integer appstate) {
        return appthirdMapper.listNew(schoolid,appstate);
    }
}
