package com.liang.controller;

import com.liang.common.base.BaseResult;
import com.liang.common.base.BaseController;
import com.liang.entity.SysDeptInfo;
import com.liang.service.SysDeptInfoService;
import com.liang.service.SysDictInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SysDeptInfo)表控制层
 *
 * @author makejava
 * @since 2023-02-26 21:19:38
 */
@Controller
@RequestMapping("/deptinfo")
public class SysDeptInfoController extends BaseController {
    final static Logger logger = LoggerFactory.getLogger(SysRoleInfoController.class);
    /**
     * 服务对象
     */
    @Resource
    private SysDeptInfoService sysDeptInfoService;
    @Resource
    private SysDictInfoService sysDictInfoService;

    /**
     * 分页查询
     *
     * @param sysDeptInfo 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<SysDeptInfo>> queryByPage(SysDeptInfo sysDeptInfo, PageRequest pageRequest) {
        return ResponseEntity.ok(this.sysDeptInfoService.queryByPage(sysDeptInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SysDeptInfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.sysDeptInfoService.queryById(id));
    }

    /**
     * 获取部门树列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list() {
        List<SysDeptInfo> list = sysDeptInfoService.list();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 查询页面初始化
     *
     * @return
     */
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "deptinfo/list";
    }


    /**
     * 部门菜单
     *
     * @return
     */
    @RequestMapping(value = "/forAdd/{deptId}", method = RequestMethod.GET)
    public String forAdd(Model model, @PathVariable Integer deptId) {
        List<Map<String, String>> dictList = sysDictInfoService.queryByCode("deptinfo_status");
        model.addAttribute("dictList", dictList);

        SysDeptInfo deptInfo = new SysDeptInfo();
        if (deptId == 0) {
            deptInfo.setDeptId(0);
            deptInfo.setAncestors("");
        } else {
            deptInfo = sysDeptInfoService.queryById(deptId);
        }
        model.addAttribute("deptInfo", deptInfo);
        return "deptinfo/add";
    }

    /**
     * 新增数据
     *
     * @param sysDeptInfo 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult doAdd(SysDeptInfo sysDeptInfo) {
        sysDeptInfo.setStatus("0");
        sysDeptInfo.setDelFlag("0");
        sysDeptInfo.setCreateUser(getSysUserId().toString());
        if (sysDeptInfo.getPhone() == null)
            sysDeptInfo.setPhone("");
        if (sysDeptInfo.getEmail() == null)
            sysDeptInfo.setEmail("");

        int num = sysDeptInfoService.insert(sysDeptInfo);
        if (num > 0) {
            return BaseResult.success("新增部门成功!");
        } else {
            return BaseResult.failure("新增部门失败!");
        }
    }

    /**
     * 部门修改页面
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate/{deptId}", method = RequestMethod.GET)
    public String forUpdate(Model model, @PathVariable Integer deptId) {
        SysDeptInfo deptInfo = sysDeptInfoService.queryById(deptId);
        // 获取下拉栏枚举值
        List<Map<String, String>> dictList = sysDictInfoService.queryByCode("deptinfo_status");
        model.addAttribute("dictList", dictList);
        model.addAttribute("deptInfo", deptInfo);
        return "deptinfo/update";
    }

    /**
     * 部门更新
     *
     * @return
     */
    @RequestMapping(value = "/doUpdate", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult doUpdate(SysDeptInfo sysDeptInfo) {

        sysDeptInfo.setUpdateUser(getSysUserId().toString());
        int num = sysDeptInfoService.update(sysDeptInfo);
        if (num > 0) {
            return BaseResult.success("部门修改成功！");
        } else {
            return BaseResult.failure("部门修改失败！");
        }
    }

    /**
     * 查看部门
     *
     * @return
     */
    @RequestMapping(value = "/forView/{deptId}", method = RequestMethod.GET)
    public String forView(Model model, @PathVariable Integer deptId) {
        SysDeptInfo deptInfo = sysDeptInfoService.queryById(deptId);
        // 获取下拉栏枚举值
        List<Map<String, String>> dictList = sysDictInfoService.queryByCode("deptinfo_status");
        model.addAttribute("dictList", dictList);
        model.addAttribute("deptInfo", deptInfo);
        return "deptinfo/view";
    }

    /**
     * 部门删除
     *
     * @return
     */
    @RequestMapping(value = "/doDelete/{deptId}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult doDelete(@PathVariable Integer deptId) {
        boolean flag = sysDeptInfoService.deleteById(deptId);
        if (flag) {
            return BaseResult.success("删除成功");
        } else {
            return BaseResult.failure("删除失败！");
        }
    }

    /**
     * 更改部门顺序
     *
     * @return
     */
    @RequestMapping(value = "/updateSort/{deptId}/{orderNum}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult updateSort(@PathVariable Integer deptId,
                                 @PathVariable Integer orderNum) {
        SysDeptInfo deptInfo = new SysDeptInfo();
        deptInfo.setDeptId(deptId);
        deptInfo.setOrderNum(orderNum);

        int num = sysDeptInfoService.update(deptInfo);
        if (num > 0) {
            return BaseResult.success("修改顺序成功!");
        } else {
            return BaseResult.failure("修改顺序失败!");
        }
    }

}