package com.yijia.station.controller;

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

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysDeptService;
import com.yijia.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
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.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.station.domain.StationPersonnel;
import com.yijia.station.service.IStationPersonnelService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 油站员工信息Controller
 *
 * @author yijia
 * @date 2020-12-09
 */
@RestController
@RequestMapping("/station/personnel")
public class StationPersonnelController extends BaseController
{
    @Autowired
    private IStationPersonnelService stationPersonnelService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询油站员工信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationPersonnel stationPersonnel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationPersonnel.setStationIdList(list);
            stationPersonnel.setStationId(null);
        }
        startPage();
        List<StationPersonnel> stationPersonnelList = stationPersonnelService.selectStationPersonnelList(stationPersonnel);
        return getDataTable(stationPersonnelList);
    }

    /**
     * 导出油站员工信息列表
     */
    @Log(title = "油站员工信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationPersonnel stationPersonnel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationPersonnel.setStationIdList(list);
            stationPersonnel.setStationId(null);
        }
        List<StationPersonnel> stationPersonnelList = stationPersonnelService.selectStationPersonnelList(stationPersonnel);
        ExcelUtil<StationPersonnel> util = new ExcelUtil<StationPersonnel>(StationPersonnel.class);
        return util.exportExcel(stationPersonnelList, "personnel");
    }

    /**
     * 获取油站员工信息详细信息
     */
    @GetMapping(value = "/{personnelId}")
    public AjaxResult getInfo(@PathVariable("personnelId") Long personnelId)
    {
        return AjaxResult.success(stationPersonnelService.selectStationPersonnelById(personnelId));
    }

    /**
     * 新增油站员工信息
     */
    @Log(title = "油站员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationPersonnel stationPersonnel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationPersonnel.setStationId(currentUser.getUser().getDeptId());
        return toAjax(stationPersonnelService.insertStationPersonnel(stationPersonnel));
    }

    /**
     * 修改油站员工信息
     */
    @Log(title = "油站员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationPersonnel stationPersonnel)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationPersonnel.setStationId(currentUser.getUser().getDeptId());

        return toAjax(stationPersonnelService.updateStationPersonnel(stationPersonnel));
    }

    /**
     * 删除油站员工信息
     */
    @Log(title = "油站员工信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{personnelIds}")
    public AjaxResult remove(@PathVariable Long[] personnelIds)
    {
        return toAjax(stationPersonnelService.deleteStationPersonnelByIds(personnelIds));
    }
}
