package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.AppConfig;
import com.winsun.mapper.AppConfigMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 应用配置
 * @author chengfengluo
 * @date 2021-02-20 10:45
 */
@RestController
@RequestMapping("/appConfig") //
public class AppConfigController extends BaseController {

    @Autowired
    private AppConfigMapper appConfigMapper;

    /**
     * 新增应用配置
     * @param appConfig
     * @return
     */
    @Permission(menuname = "添加应用配置", value = "add", method = RequestMethod.POST)
    public ResponseData<String> addAppConfig(AppConfig appConfig) {
        ShiroUser user = getShiroUser();
        if (StringUtils.isBlank(appConfig.getAppName())) {
            return ResponseData.error("应用名称不能为空");
        }

        if (StringUtils.isBlank(appConfig.getAppSecret())) {
            return ResponseData.error("应用密匙不能为空");
        }

        // 查重
        Wrapper<AppConfig> wrapper = new EntityWrapper<>();
        wrapper.eq("app_name", appConfig.getAppName());
        Integer integer = appConfigMapper.selectCount(wrapper);
        if (integer != null && integer > 0) {
            return ResponseData.error("应用名称重复");
        }

        appConfig.setCreateBy(user.getName());
        appConfig.setCreateTime(new Date());
        Integer insert = appConfigMapper.insert(appConfig);

        if (insert > 0) {
            return ResponseData.success(insert + "", "插入成功");
        }

        return ResponseData.error("插入失败");
    }

    /**
     * 编辑应用配置
     * @param appConfig
     * @return
     */
    @Permission(menuname = "更新应用配置", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updateAppConfig(AppConfig appConfig) {

        ShiroUser user = getShiroUser();

        if (appConfig.getId() == null || StringUtils.isBlank(appConfig.getAppName())) {
            return ResponseData.error("参数错误");
        }

        // 查重
        Wrapper<AppConfig> wrapper = new EntityWrapper<>();
        wrapper.eq("app_name", appConfig.getAppName());
        wrapper.ne("id", appConfig.getId());
        Integer integer = appConfigMapper.selectCount(wrapper);
        if (integer != null && integer > 0) {
            return ResponseData.error("应用名称重复");
        }
        appConfig.setUpdateBy(user.getName());
        appConfig.setUpdateTime(new Date());
        Integer integer1 = appConfigMapper.updateById(appConfig);
        if(integer1 > 0) {
            return ResponseData.success("", "更新成功");
        }
        return ResponseData.error("更新失败");
    }

    /**
     * 获取配置信息
     * @return
     */
    @GetMapping("/getConfigInfo")
    public ResponseData<AppConfig> getConfigInfo (Integer id) {

        AppConfig appConfig = appConfigMapper.selectById(id);
        return ResponseData.success(appConfig);
    }

    /**
     * 查询应用列表
     * @param appName
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseData<Page<AppConfig>> list(@RequestParam(value = "appName", required = false) String appName,
        @RequestParam(name = "pageNo", required = false) Integer pageIndex,
        @RequestParam(name = "pageSize", required = false) Integer pageSize) {

        Wrapper<AppConfig> wrapper = new EntityWrapper<>();
        wrapper.like(StringUtils.isNotBlank(appName),"app_name", appName);
        List<AppConfig> appConfigs = appConfigMapper.selectList(wrapper);

        Page<AppConfig> page = new Page<>();
        page.setTotal(appConfigs.size());
//        Integer endpageSize = (pageIndex-1) == appConfigs.size()/pageSize ? (pageIndex-1)*pageSize+appConfigs.size()%pageSize : pageIndex*pageSize;

        Integer endpageSize = pageIndex.equals(appConfigs.size()/pageSize+1) ? (pageIndex-1)*pageSize+appConfigs.size()%pageSize : pageIndex*pageSize;
        appConfigs = appConfigs.subList((pageIndex - 1)*pageSize, endpageSize);

        page.setRecords(appConfigs);
        page.setSize(appConfigs.size());

        return ResponseData.success(page, "查询成功");
    }


}
