package com.dcsoft.system.controller;

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

import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysEquipment;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.kq.service.IKqGroupUserService;
import com.dcsoft.system.service.ISysEmpowerRecordService;
import com.dcsoft.system.service.ISysEquipmentService;
import com.dcsoft.system.service.ISysPeopleService;
import com.dcsoft.system.uniubi.service.ISysSdkService;
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.SysPeopleLeave;
import com.dcsoft.system.service.ISysPeopleLeaveService;
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 nichun
 * @date 2023-09-09
 */
@RestController
@RequestMapping("/peopleLeave")
public class SysPeopleLeaveController extends BaseController
{
    @Autowired
    private ISysPeopleLeaveService sysPeopleLeaveService;

    @Autowired
    private ISysPeopleService sysPeoplService;

    @Autowired
    private IKqGroupUserService kqGroupUserService;

    @Autowired
    protected ISysSdkService sdkService;

    @Autowired
    private ISysEquipmentService equipmentService;

    @Autowired
    private ISysEmpowerRecordService empowerRecordService;


    /**
     * 查询离职管理列表
     */
    @RequiresPermissions("system:peopleLeave:list")
    @GetMapping("/list")
    public TableDataInfo list(SysPeopleLeave sysPeopleLeave)
    {
        startPage();
        List<SysPeopleLeave> list = sysPeopleLeaveService.selectSysPeopleLeaveList(sysPeopleLeave);
        return getDataTable(list);
    }

    /**
     * 查询人员
     * @param sysPeopleLeave
     * @return
     */
    @GetMapping("/queryPeopleList")
    public AjaxResult queryPeopleList(SysPeopleLeave sysPeopleLeave) {
        List<SysPeopleLeave> list = sysPeopleLeaveService.selectSysPeopleLeaveList(sysPeopleLeave);
        return success(list);
    }

    /**
     * 导出离职管理列表
     */
    @RequiresPermissions("system:peopleLeave:export")
    @Log(title = "离职管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPeopleLeave sysPeopleLeave)
    {
        List<SysPeopleLeave> list = sysPeopleLeaveService.selectSysPeopleLeaveList(sysPeopleLeave);
        ExcelUtil<SysPeopleLeave> util = new ExcelUtil<SysPeopleLeave>(SysPeopleLeave.class);
        util.exportExcel(response, list, "离职管理数据");
    }

    /**
     * 获取离职管理详细信息
     */
    @RequiresPermissions("system:peopleLeave:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysPeopleLeaveService.selectSysPeopleLeaveById(id));
    }

    /**
     * 新增离职管理
     */
    @RequiresPermissions("system:peopleLeave:add")
    @Log(title = "离职管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPeopleLeave sysPeopleLeave)
    {
        if ("1".equals(sysPeopleLeave.getStatus())){
            if(sysPeopleLeave.getPeopleId()!=null){
                sysPeoplService.deleteSysPeopleById(sysPeopleLeave.getPeopleId());
                //删除设备人员
                SysPeople people = sysPeoplService.selectSysPeopleById(sysPeopleLeave.getPeopleId());
                kqGroupUserService.deleteKqGroupUserByUserId(people.getId());
                if(StringUtils.isNotEmpty(people.getGuid())){
                    SysEquipment equipment=new SysEquipment();
                    equipment.setFlag("0");
                    List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
                    for(SysEquipment e:list){
                        sdkService.personDelete(people.getGuid(),e.getIp(),e.getPassword());
                    }
                }
            }
        }
        return toAjax(sysPeopleLeaveService.insertSysPeopleLeave(sysPeopleLeave));
    }

    /**
     * 修改离职管理
     */
    @RequiresPermissions("system:peopleLeave:edit")
    @Log(title = "离职管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPeopleLeave sysPeopleLeave)
    {
        if ("1".equals(sysPeopleLeave.getStatus())){
            if(sysPeopleLeave.getPeopleId()!=null){
                sysPeoplService.deleteSysPeopleById(sysPeopleLeave.getPeopleId());
                //删除设备人员
                SysPeople people = sysPeoplService.selectSysPeopleById(sysPeopleLeave.getPeopleId());
                kqGroupUserService.deleteKqGroupUserByUserId(people.getId());
                if(StringUtils.isNotEmpty(people.getGuid())){
                    SysEquipment equipment=new SysEquipment();
                    equipment.setFlag("0");
                    List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
                    for(SysEquipment e:list){
                        sdkService.personDelete(people.getGuid(),e.getIp(),e.getPassword());
                    }
                }
            }
        }
        return toAjax(sysPeopleLeaveService.updateSysPeopleLeave(sysPeopleLeave));
    }

    /**
     * 删除离职管理
     */
    @RequiresPermissions("system:peopleLeave:remove")
    @Log(title = "离职管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysPeopleLeaveService.deleteSysPeopleLeaveByIds(ids));
    }

    @PostMapping("/peopleLeave")
    public void peopleLeave()
    {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        SysPeopleLeave   sysPeopleLeave=new SysPeopleLeave();
        sysPeopleLeave.setType("0");
        sysPeopleLeave.setStatus("0");
        List<SysPeopleLeave> leaveList=sysPeopleLeaveService.selectSysPeopleLeaveList(sysPeopleLeave);
        for(SysPeopleLeave peopleLeave:leaveList){
            if(peopleLeave.getPeopleId()!=null){
                sysPeoplService.deleteSysPeopleById(peopleLeave.getPeopleId());
                //删除设备人员
                SysPeople people = sysPeoplService.selectSysPeopleById(peopleLeave.getPeopleId());
                kqGroupUserService.deleteKqGroupUserByUserId(people.getId());
                empowerRecordService.deleteSysEmpowerRecordByPeopleId(people.getId());
                if(StringUtils.isNotEmpty(people.getGuid())){
                    SysEquipment equipment=new SysEquipment();
                    equipment.setFlag("0");
                    List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
                    for(SysEquipment e:list){
                        sdkService.personDelete(people.getGuid(),e.getIp(),e.getPassword());
                    }
                }
            }
            peopleLeave.setStatus("1");
            sysPeopleLeaveService.updateSysPeopleLeave(peopleLeave);
        }
    }

}
