package com.cwhelp.admin.business.controller;


import com.cwhelp.admin.business.validator.BssEmployeeValid;
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.BssEmployee;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.service.BssDeptService;
import com.cwhelp.modules.business.service.BssEmployeeService;
import com.cwhelp.modules.system.domain.User;
import com.cwhelp.modules.system.service.UserService;
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/07/31
 */
@Controller
@RequestMapping("/bss/employee")
public class BssEmployeeController {

    @Autowired
    private BssEmployeeService bssEmployeeService;

    @Autowired
    private BssDeptService bssDeptService;

    @Autowired
    private UserService userService;

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

        User user = ShiroUtil.getSubject();
        List<BssDept> bssDepts = null;
        if (AdminConst.ADMIN_PLATFORM_ID.equals(user.getBssPlatform().getId())) {
            bssDepts = bssDeptService.findBssDeptByBssPlatformAndId(user.getBssPlatform());
        }
        Page<BssEmployee> list = bssEmployeeService.getPageList(bssDepts,bssEmployee);

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

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("bss:employee:add")
    public String toAdd(Model model) {
        User user = ShiroUtil.getSubject();
        BssPlatform bssPlatform = userService.getById(user.getId()).getBssPlatform();
        List<BssDept> bssDepts = getBssDepts(user, bssPlatform);
        model.addAttribute("bssDepts",bssDepts);
        return "/business/employee/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("bss:employee:edit")
    public String toEdit(@PathVariable("id") BssEmployee bssEmployee, Model model) {
        User user = ShiroUtil.getSubject();
        BssPlatform bssPlatform = userService.getById(user.getId()).getBssPlatform();
        List<BssDept> bssDepts = getBssDepts(user, bssPlatform);
        model.addAttribute("bssDepts",bssDepts);
        model.addAttribute("bssEmployee", bssEmployee);
        return "/business/employee/add";
    }

    /**
     * 获取当前用户下的部门
     * @param user
     * @param bssPlatform
     * @return
     */
    private List<BssDept> getBssDepts(User user, BssPlatform bssPlatform) {
        List<BssDept> bssDepts = null;
        if (AdminConst.ADMIN_PLATFORM_ID != user.getBssPlatform().getId()) {
            bssDepts = bssDeptService.findBssDeptByBssPlatformAndId(bssPlatform);
        } else {
            bssDepts = bssDeptService.findAll();
        }
        return bssDepts;
    }

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

        // 保存数据
        bssEmployeeService.save(bssEmployee);
        return ResultVoUtil.SAVE_SUCCESS;
    }

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

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