package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.business.entity.BusinessDictEntity;
import io.renren.modules.business.entity.BusinessPlanEntity;
import io.renren.modules.business.entity.BusinessWorkerEntity;
import io.renren.modules.business.entity.BusinessWorkerOrgEntity;
import io.renren.modules.business.service.BusinessDictService;
import io.renren.modules.business.service.BusinessPlanWorkerService;
import io.renren.modules.business.service.BusinessWorkerOrgService;
import io.renren.modules.business.service.BusinessWorkerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 工人信息表
 *
 * @author jiaqiao
 * @email jiaqiao@163.com
 * @date 2022-07-20 14:37:41
 */
@RestController
@RequestMapping("business/businessworker")
public class BusinessWorkerController {
    @Autowired
    private BusinessWorkerService businessWorkerService;
    @Autowired
    private BusinessDictService businessDictService;
    @Autowired
    private BusinessWorkerOrgService businessWorkerOrgService;
    @Autowired
    private BusinessPlanWorkerService businessPlanWorkerService;


    /**
     * 列表 含有翻页 不含有表关联
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businessworker:list")
    public R list(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        PageUtils page = businessWorkerService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取字典信息
     * @param params
     * @return
     */
    @RequestMapping("/dictList")
    @RequiresPermissions("business:businessworker:list")
    public R dictList(@RequestParam Map<String, Object> params){
        // PageUtils page = businessDictService.queryPage(params);
        List<BusinessDictEntity> businessDictEntity = businessDictService.listWithTree();
        return R.ok().put("data", businessDictEntity);
    }
    /**
     * 列表  所有未离职人员列表 不含有翻页
     */
    @RequestMapping("/tcList")
    @RequiresPermissions("business:businessworker:list")
    public R tcList(@RequestParam Map<String, Object> params) {
        List<BusinessWorkerEntity> page = businessWorkerService.queryNewPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取所有录入的员工信息
     */
    @RequestMapping("/allWorkerList")
    @RequiresPermissions("business:businessworker:list")
    public R allWorkerList() {
        List<BusinessWorkerEntity> page = businessWorkerService.allWorkerList();
        return R.ok().put("page", page);
    }

    /**
     * 获取可分配并且未离职的人员信息列表
     */
    @RequestMapping("/fpWlzList")
    @RequiresPermissions("business:businessworker:list")
    public R fpWlzList() {
        List<BusinessWorkerEntity> page = businessWorkerService.queryfpWlzList();
        return R.ok().put("page", page);
    }

    /**
     * 包含离职列表
     */
    @RequestMapping("/selectAllContainsDeparturePage")
    @RequiresPermissions("business:businessworker:list")
    public R selectAllContainsDeparturePage(@RequestParam Map<String, Object> params) {
        PageUtils page = businessWorkerService.selectAllContainsDeparturePage(params);
        return R.ok().put("page", page);
    }

    /**
     * 包含离职列表
     */
    @RequestMapping("/selectAllContainsDeparture")
    @RequiresPermissions("business:businessworker:list")
    public R selectAllContainsDeparture(@RequestParam Map<String, Object> params) {
        List<BusinessWorkerEntity> page = businessWorkerService.selectAllContainsDeparture(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取字典中位置 职位列表
     */
    @RequestMapping("/dicList")
    @RequiresPermissions("business:businessworker:list")
    public R dicList(@RequestParam Map<String, Object> params) {
        //System.out.println(params);
        List<BusinessDictEntity> businessList = businessDictService.queryWzAndZw();
        return R.ok().put("page", businessList);
    }

    /**
     * 获取人员职责信息(排除位置职位是空的数据)
     */
    @RequestMapping("/orgDutyList")
    @RequiresPermissions("business:businessworker:list")
    public R orgDutyList(@RequestParam Map<String, Object> params) {
        PageUtils page = businessWorkerService.orgDutyList(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取机构框图信息
     */
    @RequestMapping("/jgktList")
    @RequiresPermissions("business:businessworker:list")
    public R jgktList() {
        BusinessWorkerOrgEntity page = businessWorkerService.jgktList();
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("business:businessworker:info")
    public R info(@PathVariable("userId") Integer userId) {
        BusinessWorkerEntity businessWorker = businessWorkerService.getById(userId);
        return R.ok().put("data", businessWorker);
    }

    /**
     * 根据 位置id 和职位id 获取职位信息
     */
    @RequestMapping("/getOrg")
    @RequiresPermissions("business:businessworker:list")
    public R getOrg(@RequestParam Map<String, Object> params) {
        //System.out.println(params);
        BusinessWorkerOrgEntity page = businessWorkerOrgService.getOrgInfo(params);
        return R.ok().put("page", page);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessworker:save")
    public R save(@RequestBody BusinessWorkerEntity businessWorker) {
        if (businessWorker.getUserId() != null && businessWorker.getUserId() != 0) {
            businessWorkerService.updateById(businessWorker);
        } else {
            businessWorkerService.save(businessWorker);
        }

        return R.ok();
    }

    /**
     * 机构框图保存
     */
    @RequestMapping("/orgSave")
    @RequiresPermissions("business:businessworker:save")
    public R save(@RequestBody BusinessWorkerOrgEntity businessWorkerOrg){
        businessWorkerOrgService.save(businessWorkerOrg);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessworker:update")
    public R update(@RequestBody BusinessWorkerEntity businessWorker) {
        businessWorkerService.updateById(businessWorker);
        return R.ok();
    }

    /**
     * 机构框图修改
     */
    @RequestMapping("/orgUpdate")
    @RequiresPermissions("business:businessworker:update")
    public R update(@RequestBody BusinessWorkerOrgEntity businessWorkerOrg){
        businessWorkerOrgService.updateById(businessWorkerOrg);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businessworker:delete")
    public R delete(@RequestBody Integer[] userIds) {
        businessWorkerService.removeByIds(Arrays.asList(userIds));
        return R.ok();
    }

    /**
     * 职位操作
     */
    @RequestMapping("/dutyOperatee")
    @RequiresPermissions("business:businessworker:save")
    public R dutyOperate(@RequestParam Map<String, Object> params) {
        businessWorkerService.savdutyOperatee(params);
        return R.ok();
    }

    /**
     * 通过名字查条数
     */
    @RequestMapping("/findCnByName")
    @RequiresPermissions("business:businessworker:info")
    public R findCnByName(@RequestParam Map<String, Object> params){
        Integer i = 0;
        if(params.get("id")!=null&&!params.get("id").equals("")){
            String id = params.get("id").toString();
            BusinessWorkerEntity dictEntity = businessWorkerService.getById(Integer.valueOf(id));
            String name = params.get("name").toString();
            if(!dictEntity.getName().equals(name)){
                i = businessWorkerService.findCnByName(params);
            }
        }else{
            i = businessWorkerService.findCnByName(params);
        }
        return R.ok().put("data", i);
    }

    /**
     * 通过名字查条数
     */
    @RequestMapping("/queryDdry4Plan")
    @RequiresPermissions("business:businessworker:list")
    public R queryDdry4Plan(@RequestBody BusinessPlanEntity businessPlanEntity){
        List<BusinessWorkerEntity> list = businessWorkerService.queryDdry4Plan(businessPlanEntity);
        return R.ok().put("data", list);
    }

    /**
     * 通过名字查条数
     */
    @RequestMapping("/querySgry4Plan")
    @RequiresPermissions("business:businessworker:list")
    public R querySgry4Plan(@RequestParam Map<String, Object> params){
        List<BusinessWorkerEntity> list = businessWorkerService.querySgry4Plan(params);
//        list =  businessPlanWorkerService.selectDay
//		List list1 =  list.stream().collect(Collectors.collectingAndThen(
//				Collectors.toCollection(()-> new TreeSet<>(
//						Comparator.comparing(BusinessWorkerVO :: getUserId))),ArrayList::new
//		));

        return R.ok().put("list", list);
    }


}
