package com.dcsoft.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysEquipment;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.SysPeopleRule;
import com.dcsoft.system.domain.vo.JjserInfoVo;
import com.dcsoft.system.service.ISysPeopleRuleService;
import com.dcsoft.system.service.ISysPeopleService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.domain.SysBranch;
import com.dcsoft.system.service.ISysBranchService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;

/**
 * 部门管理Controller
 *
 * @author dcsoft
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/sysBranch")
public class SysBranchController extends BaseController
{
    @Autowired
    private ISysBranchService sysBranchService;

    @Autowired
    private ISysPeopleService sysPeopleService;


    @Autowired
    private ISysPeopleRuleService sysPeopleRuleService;

    /**
     * 查询部门管理列表
     */
    @RequiresPermissions("system:sysBranch:list")
    @GetMapping("/list")
    public AjaxResult list(SysBranch sysBranch)
    {
        List<SysBranch> list = sysBranchService.selectSysBranchList(sysBranch);
        return success(list);
    }

    /**
     * 查询部门管理列表
     */
    @RequiresPermissions("system:sysBranch:list")
    @GetMapping("/pageList")
    public TableDataInfo pageList(SysBranch sysBranch)
    {
        startPage();
        List<SysBranch> list = sysBranchService.selectSysBranchList(sysBranch);
        return getDataTable(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @RequiresPermissions("system:sysBranch:list")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) String id)
    {
        List<SysBranch> depts = sysBranchService.selectSysBranchList(new SysBranch());
        depts.removeIf(d -> d.getId().equals(id) || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return success(depts);
    }

    /**
     * 导出部门管理列表
     */
    @RequiresPermissions("system:sysBranch:export")
    @Log(title = "部门管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBranch sysBranch)
    {
        List<SysBranch> list = sysBranchService.selectSysBranchList(sysBranch);
        ExcelUtil<SysBranch> util = new ExcelUtil<SysBranch>(SysBranch.class);
        util.exportExcel(response, list, "部门管理数据");
    }

    /**
     * 获取部门管理详细信息
     */
    @RequiresPermissions("system:sysBranch:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysBranchService.selectSysBranchById(id));
    }

    /**
     * 新增部门管理
     */
    @RequiresPermissions("system:sysBranch:add")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBranch sysBranch)
    {   sysBranch.setId(UUID.randomUUID().toString());
        return toAjax(sysBranchService.insertSysBranch(sysBranch));
    }

    /**
     * 修改部门管理
     */
    @RequiresPermissions("system:sysBranch:edit")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBranch sysBranch)
    {
        return toAjax(sysBranchService.updateSysBranch(sysBranch));
    }

    /**
     * 删除部门管理
     */
    @RequiresPermissions("system:sysBranch:remove")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysBranchService.deleteSysBranchByIds(ids));
    }

    /**
     * 获取空间树列表
     */
//    @RequiresPermissions("system:sysBranch:list")
    @GetMapping("/branchTree")
    public AjaxResult branchTree(SysBranch space)
    {
        return success(sysBranchService.selectBranchTreeList(space));
    }

    @RequiresPermissions("system:sysBranch:list")
    @GetMapping("/queryBranchTree")
    public AjaxResult queryBranchTree(SysBranch space)
    {
        return success(sysBranchService.queryBranchTree(space));
    }

    /**
     * 同步部门（锦江）
     */
    @GetMapping("/saveBranchTree")
    public void saveBranchTree(SysBranch space) {
        sysBranchService.saveBranchTree(space);
    }

    /**
     * 同步岗位（锦江）
     */
    @GetMapping("/saveStation")
    public void saveStation(SysBranch space) {
        sysBranchService.saveStation(space);
    }


    /**
     * 获取设备信息详细信息
     */
    @GetMapping(value = "/down/{id}")
    public AjaxResult down(@PathVariable("id") Long id)
    {
        SysPeople people=new SysPeople();
        people.setBranchParent(id);
        List<SysPeople> peopleList=sysPeopleService.selectSysPeopleList(people);
        for(SysPeople p:peopleList){
            if(p.getBranch()!=null){
                SysPeopleRule sysPeopleRule=new SysPeopleRule();
                sysPeopleRule.setRuleId(p.getBranch().getRuleId());
                sysPeopleRule.setPeopleId(p.getId());
                sysPeopleRuleService.insertSysPeopleRule(sysPeopleRule);
                p.setDown(0L);
                sysPeopleService.updateSysPeople(p);
            }
        }
        return success(peopleList);
    }



}
