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.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.station.domain.StationOilPriceAdjust;
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.StationOilPrice;
import com.yijia.station.service.IStationOilPriceService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 油品价格Controller
 *
 * @author yijia
 * @date 2020-12-08
 */
@RestController
@RequestMapping("/station/price")
public class StationOilPriceController extends BaseController
{
    @Autowired
    private IStationOilPriceService stationOilPriceService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询油品价格列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationOilPrice stationOilPrice)
    {
        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){
            stationOilPrice.setStationIdList(list);
            stationOilPrice.setStationId(null);
        }
        startPage();
        List<StationOilPrice> priceList = stationOilPriceService.selectStationOilPriceList(stationOilPrice);
        return getDataTable(priceList);
    }

    /**
     * 导出油品价格列表
     */
    @Log(title = "油品价格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationOilPrice stationOilPrice)
    {
        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){
            stationOilPrice.setStationIdList(list);
            stationOilPrice.setStationId(null);
        }
        List<StationOilPrice> priceList = stationOilPriceService.selectStationOilPriceList(stationOilPrice);
        ExcelUtil<StationOilPrice> util = new ExcelUtil<StationOilPrice>(StationOilPrice.class);
        return util.exportExcel(priceList, "price");
    }

    /**
     * 获取油品价格详细信息
     */
    @GetMapping(value = "/{oilPriceId}")
    public AjaxResult getInfo(@PathVariable("oilPriceId") Long oilPriceId)
    {
        return AjaxResult.success(stationOilPriceService.selectStationOilPriceById(oilPriceId));
    }

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

    /**
     * 修改油品价格
     */
    @Log(title = "油品价格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationOilPrice stationOilPrice)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationOilPrice.setStationId(currentUser.getUser().getDeptId());
        stationOilPrice.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(stationOilPriceService.updateStationOilPrice(stationOilPrice));
    }

    /**
     * 删除油品价格
     */
    @Log(title = "油品价格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{oilPriceIds}")
    public AjaxResult remove(@PathVariable Long[] oilPriceIds)
    {
        return toAjax(stationOilPriceService.deleteStationOilPriceByIds(oilPriceIds));
    }

    /**
     * 查询当前油站对应的油品名称
     */
    @GetMapping("/oilNameList")
    public TableDataInfo oilNameList(StationOilPrice stationOilPrice)
    {
        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){
            stationOilPrice.setStationIdList(list);
            stationOilPrice.setStationId(null);
        }
        List<StationOilPrice> priceList = stationOilPriceService.selectOilPriceList(stationOilPrice);
        return getTable(priceList);
    }
    /**
     * 查询当前油站对应的油品名称(不包括非油品)
     */
    @GetMapping("/oilNameInfo")
    public TableDataInfo oilNameInfo(StationOilPrice stationOilPrice)
    {
        List<StationOilPrice> list = stationOilPriceService.oilNameInfo(stationOilPrice);
        return getTable(list);
    }
}
