package com.cwhelp.admin.business.controller;

import com.cwhelp.admin.business.validator.BssClassItemValid;
import com.cwhelp.common.enums.AccountStandardEnum;
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.BssClassItem;
import com.cwhelp.modules.business.service.BssClassItemService;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huang.jc
 * @date 2019/12/11
 */
@Controller
@RequestMapping("/bss/classItem")
public class BssClassItemController {

    @Autowired
    private BssClassItemService bssClassItemService;

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

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

        // 获取数据列表
        Example<BssClassItem> example = Example.of(classItem, matcher);
        Page<BssClassItem> list = bssClassItemService.getPageList(example);
        List<BssClassItem> items = list.getContent();
        items.stream().forEach(bssClassItem -> {
            bssClassItem.setItemTypeName(AccountStandardEnum.getNameById(bssClassItem.getItemType()));
        });

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

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("bss:classItem:add")
    public String toAdd() {
        return "/business/classItem/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("bss:classItem:edit")
    public String toEdit(@PathVariable("id") BssClassItem classItem, Model model) {
        model.addAttribute("classItem", classItem);
        return "/business/classItem/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping({"/add","/edit"})
    @RequiresPermissions({"bss:classItem:add","bss:classItem:edit"})
    @ResponseBody
    public ResultVo save(@Validated BssClassItemValid valid, BssClassItem classItem) {



        // 复制保留无需修改的数据
        if (classItem.getId() != null) {
            BssClassItem beClassItem = bssClassItemService.getById(classItem.getId());
            EntityBeanUtil.copyProperties(beClassItem, classItem);
        }

        // 保存数据
        bssClassItemService.save(classItem);
        return ResultVoUtil.SAVE_SUCCESS;
    }

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

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