package com.yijia.station.controller;

import java.util.Date;
import java.util.List;

import com.yijia.station.domain.PayOrder;
import com.yijia.station.mapper.PayOrderMapper;
import com.yijia.station.service.IPayOrderService;
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.StationClassStructure;
import com.yijia.station.service.IStationClassStructureService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 班结管理Controller
 * 
 * @author yijia
 * @date 2020-12-14
 */
@RestController
@RequestMapping("/station/structure")
public class StationClassStructureController extends BaseController
{
    @Autowired
    private IStationClassStructureService stationClassStructureService;
    @Autowired
    private IPayOrderService payOrderService;
    /**
     * 查询班结管理列表
     */
    @PreAuthorize("@ss.hasPermi('station:structure:list')")
    @GetMapping("/list")
    public TableDataInfo list(StationClassStructure stationClassStructure)
    {
        startPage();
        List<StationClassStructure> list = stationClassStructureService.selectStationClassStructureList(stationClassStructure);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('station:structure:listPersonnelStructure')")
    @GetMapping("/listPersonnelStructure")
    public TableDataInfo listPersonnelStructure(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = stationClassStructureService.listPersonnelStructure(stationClassStructure);
        return getTable(list);
    }

    @PreAuthorize("@ss.hasPermi('station:structure:listOilStructure')")
    @GetMapping("/listOilStructure")
    public TableDataInfo listOilStructure(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = stationClassStructureService.listOilStructure(stationClassStructure);
        return getTable(list);
    }
    /**
     * 导出班结管理列表
     */
    @PreAuthorize("@ss.hasPermi('station:structure:export')")
    @Log(title = "班结管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = stationClassStructureService.selectStationClassStructureList(stationClassStructure);
        ExcelUtil<StationClassStructure> util = new ExcelUtil<StationClassStructure>(StationClassStructure.class);
        return util.exportExcel(list, "structure");
    }

    /**
     * 获取班结管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('station:structure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stationClassStructureService.selectStationClassStructureById(id));
    }

    /**
     * 新增班结管理
     */
    @PreAuthorize("@ss.hasPermi('station:structure:add')")
    @Log(title = "班结管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationClassStructure stationClassStructure){
        //获取当前班结油站
        // Long stationId=stationClassStructure.getStationId();
        Long stationId= Long.valueOf("1");
        String stationName=stationClassStructure.getStationName();
        String username = stationClassStructure.getClassStructureMan();
        //根据油站查询出油站是否班结过，班结过就需要查询上次班结的时间
        StationClassStructure classes =new StationClassStructure();
        classes.setStationId(stationId);
        //查询出上次班结，最后一条数据
        StationClassStructure sss =stationClassStructureService.selectStationClassStructureMax(classes);
        //当前时间
        Date date= new Date();
        int i=0;

        if(sss!=null){
            //获取上次班结时间
            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            order.setBeginTime(sss.getClassStructureDate().toString());
            order.setEndTime(date.toString());
            List<PayOrder> list = payOrderService.selectPayOrderList(order);
            for(PayOrder payOrder:list){
                StationClassStructure stationclass =new StationClassStructure();
                stationclass.setStationId(payOrder.getStationId());
                stationclass.setClassStructureMan(username);
                stationclass.setClassStructureDate(date);
                stationclass.setOrderNo(payOrder.getOrderNo());
                stationclass.setOilGun(payOrder.getOilGun());
                stationclass.setOilName(payOrder.getOilName());
                stationclass.setConsumerId(payOrder.getConsumerId());
                stationclass.setConsumer(payOrder.getConsumer());
                stationclass.setAmt(payOrder.getAmt());
                stationclass.setOrderLiters(payOrder.getOrderLiters());
                stationclass.setPayWay(payOrder.getPayWay());
                stationclass.setPayType(payOrder.getPayType());
                stationclass.setPayDate(payOrder.getPayDate());
                stationclass.setOilPersonnel(payOrder.getOilPersonnel());
                stationclass.setOrderType(payOrder.getOrderType());
                //stationclass.setOilType();
                //stationclass.getPrintNum();
                //stationclass
                i= stationClassStructureService.insertStationClassStructure(stationclass);
            }
        }else{
            //初次班结 班结时间默认为当前时间，班结人username
            //根据班结时间查询订单

            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            order.setEndTime(date.toString());
            List<PayOrder> list = payOrderService.selectPayOrderList(order);
            for(PayOrder payOrder:list){
                StationClassStructure stationclass =new StationClassStructure();
                stationclass.setStationId(payOrder.getStationId());
                stationclass.setClassStructureMan(username);
                stationclass.setClassStructureDate(date);
                stationclass.setOrderNo(payOrder.getOrderNo());
                stationclass.setOilGun(payOrder.getOilGun());
                stationclass.setOilName(payOrder.getOilName());
                stationclass.setConsumerId(payOrder.getConsumerId());
                stationclass.setConsumer(payOrder.getConsumer());
                stationclass.setAmt(payOrder.getAmt());
                stationclass.setOrderLiters(payOrder.getOrderLiters());
                stationclass.setPayWay(payOrder.getPayWay());
                stationclass.setPayType(payOrder.getPayType());
                stationclass.setPayDate(payOrder.getPayDate());
                stationclass.setOilPersonnel(payOrder.getOilPersonnel());
                stationclass.setOrderType(payOrder.getOrderType());
                //stationclass.setOilType();
                //stationclass.getPrintNum();
                //stationclass
                i= stationClassStructureService.insertStationClassStructure(stationclass);
            }
        }
        return toAjax(i);

    }

    /**
     * 修改班结管理
     */
    @PreAuthorize("@ss.hasPermi('station:structure:edit')")
    @Log(title = "班结管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationClassStructure stationClassStructure)
    {
        return toAjax(stationClassStructureService.updateStationClassStructure(stationClassStructure));
    }

    /**
     * 删除班结管理
     */
    @PreAuthorize("@ss.hasPermi('station:structure:remove')")
    @Log(title = "班结管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stationClassStructureService.deleteStationClassStructureByIds(ids));
    }
}
