package com.sysu.admin.support.system.config;

import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.sysu.admin.support.base.BaseVo;
import com.sysu.admin.support.base.ServiceContext;
import com.xiesx.fastboot.base.pagination.PaginationHelper;
import com.xiesx.fastboot.base.pagination.PaginationResult;
import com.xiesx.fastboot.base.pagination.PaginationVo;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import com.xiesx.fastboot.core.exception.RunException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @title ConfigController
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:02:30
 */
@Validated
@Controller
@RequestMapping(value = "config/")
public class SConfigController extends ServiceContext {

    /**
     * 页面
     *
     * @return
     */
    @RequestMapping(value = "index")
    @RequiresPermissions("config:view")
    public String index() {
        return "system/config/index";
    }

    /**
     * 分页
     *
     * @param base
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "page")
    @RequiresPermissions("config:select")
    public PaginationResult page(BaseVo base, PaginationVo page) {
        // 对象
        QSConfig qConfig = QSConfig.sConfig;
        // 条件
        Predicate predicate = qConfig.k.isNotNull();
        if (ObjectUtils.isNotEmpty(base.getKeyword())) {
            Predicate p1 = qConfig.title.likeIgnoreCase("%" + base.getKeyword() + "%");
            Predicate p2 = qConfig.k.likeIgnoreCase("%" + base.getKeyword() + "%");
            predicate = ExpressionUtils.and(predicate, ExpressionUtils.anyOf(p1, p2));
        }
        // 排序
        Sort sort = Sort.by(Direction.ASC, SConfig.FIELDS.sort);
        // 分页
        Pageable pageable = PageRequest.of(page.getPage(), page.getLimit(), sort);
        // 查询数据
        Page<SConfig> data = mSConfigService.findAll(predicate, pageable);
        // 构造
        return PaginationHelper.create(data);
    }

    /**
     * 详情
     *
     * @param model
     * @return
     * @throws RunException
     */
    @RequestMapping(value = "form")
    @RequiresPermissions("config:select")
    public String form(String k, Model model) {
        SConfig config = mSConfigService.get(k);
        if (ObjectUtils.isEmpty(config)) {
            config = new SConfig();
        }
        model.addAttribute("json", config.toJSONString());
        return "system/config/form";
    }

    /**
     * 添加/修改
     *
     * @param base
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "submit")
    @RequiresPermissions(value = {"config:insert", "config:update"}, logical = Logical.OR)
    public BaseResult submit(BaseVo base, SConfig req) {
        SConfig config = mSConfigService.get(req.getK());
        if (ObjectUtils.isEmpty(config)) {
            return R.succ((mSConfigService.insertOrUpdate(req) == 1) ? "添加成功" : "添加失败");
        } else {
            return R.succ((mSConfigService.insertOrUpdate(req) == 1) ? "修改成功" : "修改失败");
        }
    }


}
