package com.yijia.station.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.entity.SysRole;
import com.yijia.common.core.domain.entity.SysUser;
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.StationOilGun;
import com.yijia.station.service.IStationOilGunService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 油枪管理Controller
 *
 * @author yijia
 * @date 2020-12-09
 */
@RestController
@RequestMapping("/station/gun")
public class StationOilGunController extends BaseController
{
    @Autowired
    private IStationOilGunService stationOilGunService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询油枪管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationOilGun stationOilGun)
    {
        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){
            stationOilGun.setStationIdList(list);
            stationOilGun.setStationId(null);
        }
        startPage();
        List<StationOilGun> stationOilGunList = stationOilGunService.selectStationOilGunList(stationOilGun);
        return getDataTable(stationOilGunList);
    }

    /**
     * 导出油枪管理列表
     */
    @Log(title = "油枪管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationOilGun stationOilGun)
    {
        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){
            stationOilGun.setStationIdList(list);
            stationOilGun.setStationId(null);
        }
        List<StationOilGun> stationOilGunList = stationOilGunService.selectStationOilGunList(stationOilGun);
        ExcelUtil<StationOilGun> util = new ExcelUtil<StationOilGun>(StationOilGun.class);
        return util.exportExcel(stationOilGunList, "gun");
    }

    /**
     * 获取油枪管理详细信息
     */
    @GetMapping(value = "/selectGun")
    public TableDataInfo selectGun(StationOilGun stationOilGun)
    {
        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){
            stationOilGun.setStationIdList(list);
            stationOilGun.setStationId(null);
        }
        List<StationOilGun> stationOilGunList = stationOilGunService.selectStationOilGunList(stationOilGun);
        return getTable(stationOilGunList);
    }

    /**
     * 根据油站id查询油枪
     */
    @GetMapping(value = "/{oilGunId}")
    public AjaxResult getInfo(@PathVariable("oilGunId") Long oilGunId)
    {
        return AjaxResult.success(stationOilGunService.selectStationOilGunById(oilGunId));
    }

    /**
     * 新增油枪管理
     */
    @Log(title = "油枪管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationOilGun stationOilGun) throws ParseException {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationOilGun.setStationId(currentUser.getUser().getDeptId());
        stationOilGun.setStationName(currentUser.getUser().getDept().getDeptName());
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);
        Date  now = sdf.parse(time);
        stationOilGun.setDate(now);
        return toAjax(stationOilGunService.insertStationOilGun(stationOilGun));
    }

    /**
     * 修改油枪管理
     */
    @Log(title = "油枪管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationOilGun stationOilGun)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationOilGun.setStationId(currentUser.getUser().getDeptId());
        stationOilGun.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(stationOilGunService.updateStationOilGun(stationOilGun));
    }

    /**
     * 删除油枪管理
     */
    @Log(title = "油枪管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{oilGunIds}")
    public AjaxResult remove(@PathVariable Long[] oilGunIds)
    {
        return toAjax(stationOilGunService.deleteStationOilGunByIds(oilGunIds));
    }
}
