package com.cwhelp.admin.business.controller;

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.BssTaxinfo;
import com.cwhelp.modules.business.service.BssTaxinfoService;
import com.cwhelp.admin.business.validator.BssTaxinfoValid;
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 yan.y
 * @date 2019/08/16
 */
@Controller
@RequestMapping("/bss/taxinfo")
public class BssTaxinfoController {

    @Autowired
    private BssTaxinfoService bssTaxinfoService;

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

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

        bssTaxinfo.setPlatformId(ShiroUtil.getSubject().getBssPlatform().getId());
        // 获取数据列表
        Example<BssTaxinfo> example = Example.of(bssTaxinfo, matcher);
        Page<BssTaxinfo> list = bssTaxinfoService.getPageList(example);

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

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

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

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping({"/add","/edit"})
    @RequiresPermissions({"bss:taxinfo:add","bss:taxinfo:edit"})
    @ResponseBody
    public ResultVo save(@Validated BssTaxinfoValid valid, BssTaxinfo bssTaxinfo) {
        bssTaxinfo.setPlatformId(ShiroUtil.getSubject().getBssPlatform().getId());
        // 复制保留无需修改的数据
        if (bssTaxinfo.getId() != null) {
            BssTaxinfo beBssTaxinfo = bssTaxinfoService.getById(bssTaxinfo.getId());
            EntityBeanUtil.copyProperties(beBssTaxinfo, bssTaxinfo);
        }

        // 保存数据
        bssTaxinfoService.save(bssTaxinfo);
        return ResultVoUtil.SAVE_SUCCESS;
    }

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

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