package com.cwhelp.admin.business.controller;

import com.cwhelp.admin.business.validator.BssPlatformValid;
import com.cwhelp.common.enums.ResultEnum;
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.BssPlatform;
import com.cwhelp.modules.business.service.BssPlatformService;
import com.cwhelp.modules.system.domain.Dept;
import org.apache.shiro.authz.annotation.Logical;
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/28
 */
@Controller
@RequestMapping("/bss/platform")
public class BssPlatformController {

    @Autowired
    private BssPlatformService bssPlatformService;

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

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

        // 获取数据列表
        Example<BssPlatform> example = Example.of(bssPlatform, matcher);
        Page<BssPlatform> list = bssPlatformService.getPageList(example);

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

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

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

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping({"/add","/edit"})
    @RequiresPermissions({"bss:platform:add","bss:platform:edit"})
    @ResponseBody
    public ResultVo save(@Validated BssPlatformValid valid, BssPlatform bssPlatform) {
        // 复制保留无需修改的数据
        if (bssPlatform.getId() != null) {
            BssPlatform beBssPlatform = bssPlatformService.getById(bssPlatform.getId());
            EntityBeanUtil.copyProperties(beBssPlatform, bssPlatform);
        }
        // 校验手机号
        boolean repeatByContactTel = bssPlatformService.repeatByContactTel(bssPlatform);
        if (repeatByContactTel) {
            return ResultVoUtil.error(ResultEnum.PLATFORM_EXIST_CONTACT_TEL.getCode(),ResultEnum.PLATFORM_EXIST_CONTACT_TEL.getMessage());
        }
        // 校验身份证号
        boolean repeatByContactCard = bssPlatformService.repeatByContactCard(bssPlatform);
        if (repeatByContactCard) {
            return ResultVoUtil.error(ResultEnum.PLATFORM_EXIST_CONTACT_CARD.getCode(),ResultEnum.PLATFORM_EXIST_CONTACT_CARD.getMessage());
        }
        // 校验邮箱
        boolean repeatByEmail = bssPlatformService.repeatByEmail(bssPlatform);
        if (repeatByEmail) {
            return ResultVoUtil.error(ResultEnum.PLATFORM_EXIST_EMAIL.getCode(),ResultEnum.PLATFORM_EXIST_EMAIL.getMessage());
        }

        // 保存数据
        bssPlatformService.save(bssPlatform);
        return ResultVoUtil.SAVE_SUCCESS;
    }

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

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