package com.cwhelp.admin.business.controller;

import com.cwhelp.admin.business.validator.BssDeptValid;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.common.utils.EntityBeanUtil;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.utils.StatusUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.service.BssDeptService;
import com.cwhelp.modules.business.service.BssPlatformService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yan.y
 * @date 2019/07/29
 */
@RequestMapping("/bss/dept")
public class BssDeptController {

    @Autowired
    private BssDeptService bssDeptService;

    @Autowired
    private BssPlatformService bssPlatformService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("bss:dept:index")
    public String index(Model model, BssDept bssDept) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", match -> match.contains())
                .withMatcher("bssPlatform.name",match -> match.contains());

        // 获取数据列表
        Example<BssDept> example = Example.of(bssDept, matcher);
        Page<BssDept> list = bssDeptService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/business/dept/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("bss:dept:add")
    public String toAdd(Model model,BssPlatform bssPlatform) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<BssPlatform> example = Example.of(bssPlatform, matcher);
        List<BssPlatform> list = bssPlatformService.getListByExample(example, sort);
        model.addAttribute("bssPlatforms",list);
        return "/business/dept/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("bss:dept:edit")
    public String toEdit(@PathVariable("id") BssDept bssDept, Model model) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        ExampleMatcher matcher = ExampleMatcher.matching();
        Example<BssPlatform> example = Example.of(new BssPlatform(), matcher);
        List<BssPlatform> list = bssPlatformService.getListByExample(example, sort);
        model.addAttribute("bssPlatforms",list);
        model.addAttribute("dept", bssDept);
        return "/business/dept/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping({"/add","/edit"})
    @RequiresPermissions({"bss:dept:add","bss:dept:edit"})
    @ResponseBody
    public ResultVo save(@Validated BssDeptValid valid, BssDept bssDept) {
        // 复制保留无需修改的数据
        if (bssDept.getId() != null) {
            BssDept beBssDept = bssDeptService.getById(bssDept.getId());
            EntityBeanUtil.copyProperties(beBssDept, bssDept);
        }

        // 保存数据
        bssDeptService.save(bssDept);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("bss:dept:detail")
    public String toDetail(@PathVariable("id") BssDept bssDept, Model model) {
        model.addAttribute("dept", bssDept);
        return "/business/dept/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("bss:dept:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (bssDeptService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}