package com.liang.controller;

import com.liang.common.base.BaseController;
import com.liang.common.enums.PostInfoEnum;
import com.liang.entity.SysPostInfo;
import com.liang.entity.SysUserInfo;
import com.liang.entity.SysUserPost;
import com.liang.service.SysPostInfoService;
import com.liang.service.SysUserPostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

import static com.liang.common.utils.SecurityUtil.getSysUserInfo;

/**
 * (SysUserPost)表控制层
 *
 * @author makejava
 * @since 2023-03-17 09:33:49
 */
@RestController
@RequestMapping("sysUserPost")
public class SysUserPostController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserPostService sysUserPostService;
    @Resource
    private SysPostInfoService sysPostInfoService;

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

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

    /**
     * 新增数据
     *
     * @param sysUserPost 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SysUserPost> add(SysUserPost sysUserPost) {
        return ResponseEntity.ok(this.sysUserPostService.insert(sysUserPost));
    }

    /**
     * 编辑数据
     *
     * @param sysUserPost 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<SysUserPost> edit(SysUserPost sysUserPost) {
        return ResponseEntity.ok(this.sysUserPostService.update(sysUserPost));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sysUserPostService.deleteById(id));
    }

    /**
     * 用户岗位查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/postUserPostlist", method = RequestMethod.GET)
    public Object postUserPostlist() {
//岗位
        Map<String, String> paraMap = new HashMap<>();
//系统当前用户
        SysUserInfo userInfo = getSysUserInfo();
        if (userInfo != null && userInfo.getDeptId().equals("52")) {
            paraMap.put("postName", PostInfoEnum.KJBBMZR.postName());
        } else {
            paraMap.put("postName", PostInfoEnum.KYFZR.postName());
        }

        List<SysPostInfo> postInfo = sysPostInfoService.getSysPostInfo1(paraMap);

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

        return resultMap;
    }


    /*
     * 用户岗位列表
     */
    @ResponseBody
    @RequestMapping(value = "/postUserlist/{postId}", method = RequestMethod.GET)
    public Object postUserlist(@PathVariable Integer postId,
                               @RequestParam(required = false, defaultValue = "", value = "userName") String userName) {
//系统当前用户
        SysPostInfo sysPostInfo = sysPostInfoService.queryById(postId);
        String postName = sysPostInfo.getPostName();
        SysUserInfo userInfo = getSysUserInfo();
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("postId", postId.toString());
        paraMap.put("userName", userName);
        paraMap.put("postName",postName);
        String deptName = userInfo.getDeptName();
        if(deptName.contains("电源")){
            deptName = null;
        }
        paraMap.put("deptName", deptName);
        List<SysUserPost> list = sysUserPostService.getPostUserList(paraMap);
        List<SysUserPost> list_power = new ArrayList<>();
//如果是电源相关部门就只传一个李华
        Map<String, Object> resultMap = new HashMap<>();
//电源相关部门全部提交给李华审核
        if (userInfo.getDeptName().contains("电源")) {
            for (int i = 0; i < list.size(); i++) {
                if ("李华".equals(list.get(i).getName())) {
                    list_power.add(list.get(i));
                    resultMap.put("code", 0);
                    resultMap.put("msg", "");
                    resultMap.put("count", list_power.size());
                    resultMap.put("data", list_power);

                    return resultMap;
                }
            }
        }
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }
}

