package com.cwhelp.admin.business.controller;

import com.cwhelp.admin.business.validator.BssDeptValid;
import com.cwhelp.common.constant.AdminConst;
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.component.shiro.ShiroUtil;
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 com.cwhelp.modules.system.domain.User;
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
 */
@Controller
@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());

        User user = ShiroUtil.getSubject();
        if (AdminConst.ADMIN_PLATFORM_ID != user.getBssPlatform().getId()) {
            matcher.withMatcher("bssPlatform.id",match -> match.contains());
            BssPlatform bssPlatform = new BssPlatform();
            bssPlatform.setId(user.getBssPlatform().getId());
            bssDept.setBssPlatform(bssPlatform);
        }
        // 获取数据列表
        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) {
        List<BssPlatform> list = getBssPlatforms(bssPlatform);
        model.addAttribute("bssPlatforms",list);
        return "/business/dept/add";
    }

    private List<BssPlatform> getBssPlatforms(BssPlatform bssPlatform) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        Example<BssPlatform> example = Example.of(bssPlatform, ExampleMatcher.matching());
        return bssPlatformService.getListByExample(example, sort);
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("bss:dept:edit")
    public String toEdit(@PathVariable("id") BssDept bssDept, Model model) {
        List<BssPlatform> list = getBssPlatforms(new BssPlatform());
        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() + "失败，请重新操作");
        }
    }
}