package com.yijia.station.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yijia.station.domain.PayOrder;
import com.yijia.station.domain.StationClassesSummary;
import com.yijia.station.mapper.PayOrderMapper;
import com.yijia.station.service.IPayOrderService;
import com.yijia.station.service.IStationClassesSummaryService;
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-31
 */
@RestController
@RequestMapping("/station/structure")
public class StationClassStructureController extends BaseController
{
    @Autowired
    private IStationClassStructureService stationClassStructureService;
    @Autowired
    private IPayOrderService payOrderService;
    @Autowired
    private IStationClassesSummaryService stationClassesSummaryService;
    /**
     * 查询班结管理列表
     */
    @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 = new ArrayList<>();
        if(stationClassStructure!=null &&stationClassStructure.getStationId()!=null ){
          list = stationClassStructureService.listPersonnelStructure(stationClassStructure);
        }
        return getTable(list);
    }

    @PreAuthorize("@ss.hasPermi('station:structure:listOilStructure')")
    @GetMapping("/listOilStructure")
    public TableDataInfo listOilStructure(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = new ArrayList<>();
        if(stationClassStructure!=null &&stationClassStructure.getStationId()!=null ){
            list = stationClassStructureService.listOilStructure(stationClassStructure);
        }
        return getTable(list);
    }
    @PreAuthorize("@ss.hasPermi('station:structure:listPersonnelPayStructure')")
    @GetMapping("/listPersonnelPayStructure")
    public TableDataInfo listPersonnelPayStructure(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = new ArrayList<>();
        if(stationClassStructure!=null &&stationClassStructure.getStationId()!=null ){
            list = stationClassStructureService.listPersonnelPayStructure(stationClassStructure);
        }
        return getTable(list);
    }
    @PreAuthorize("@ss.hasPermi('station:structure:listClass')")
    @GetMapping("/listClass")
    public AjaxResult listClass(StationClassStructure stationClassStructure)
    {

        return AjaxResult.success(stationClassStructureService.listClass(stationClassStructure));
    }
    /**
     * 导出班结管理列表
     */
    @PreAuthorize("@ss.hasPermi('station:structure:export')")
    @Log(title = "班结管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationClassStructure stationClassStructure)
    {
        List<StationClassStructure> list = stationClassStructureService.selectStationClassStructureExport(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) throws ParseException {
        //获取当前班结油站
        // 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);
        String beginTime="";
        //当前时间
        Date date= new Date();
        StationClassStructure classesStructure =new StationClassStructure();
        classesStructure.setStationId(stationId);
        DateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
        String no=dFormat.format(date);
        classesStructure.setLikeClassStructureNo(no);
        //查询班结信息中本日班结次数
        StationClassStructure structure =stationClassStructureService.selectDistnctClassStructure(classesStructure);
        if(structure!=null){
            if(structure.getNum()==0){
                no = no + "0"+1;
            }else{
                int num  = structure.getNum()+1;
                if(num>0 && num<10){
                    no = no + "0"+num;
                }else{
                    no = no +""+ num;
                }
            }
        }
        int i=0;
        if(sss!=null){
            //获取上次班结时间
            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            beginTime= dateFormat.format(sss.getClassStructureDate());
            order.setBeginTime(beginTime);
            order.setEndTime( dateFormat.format(date));
            SimpleDateFormat dfe = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<PayOrder> list = payOrderService.selectPayOrderList(order);
            for(PayOrder payOrder:list){
                StationClassStructure stationclass =new StationClassStructure();
                stationclass.setStationId(payOrder.getStationId());
                stationclass.setStationName(payOrder.getStationName());
                stationclass.setClassStartDate(dfe.parse(beginTime));
                stationclass.setClassStructureMan(username);
                stationclass.setClassStructureNo(no);
                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());
                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.setStationName(payOrder.getStationName());
                stationclass.setClassStructureMan(username);
                stationclass.setClassStructureDate(date);
                stationclass.setClassStructureNo(no);
                stationclass.setOrderNo(payOrder.getOrderNo());
                stationclass.setOilGun(payOrder.getOilGun());
                stationclass.setOilName(payOrder.getOilName());
                stationclass.setConsumerId(payOrder.getConsumerId());
                stationclass.setConsumer(payOrder.getConsumer());
                stationclass.setAmt(payOrder.getAmt());
                if(payOrder.getOrderLiters()!=null){
                    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(payOrder.getOilType());
                stationclass.setPrintNum(payOrder.getPrintCount());
                i= stationClassStructureService.insertStationClassStructure(stationclass);
            }
        }
        StationClassStructure scs =new StationClassStructure();
        scs.setClassStructureNo(no);
        //汇总班结数据将汇总的数据插入到汇总数据中
        StationClassStructure stationclass = stationClassStructureService.selectSummyClassStructure(scs);
        StationClassesSummary stationClassesSummary =new StationClassesSummary();
        stationClassesSummary.setClassesNo(stationclass.getClassStructureNo());
        if(stationclass.getClassStartDate()!=null){
            stationClassesSummary.setStartDate(stationclass.getClassStartDate());
        }
        if(stationclass.getClassStructureDate()!=null){
            stationClassesSummary.setEndDate(stationclass.getClassStructureDate());
        }

        stationClassesSummary.setStationName(stationclass.getStationName());
        if(stationclass.getStationId()!=null){
            stationClassesSummary.setStationId(stationclass.getStationId());
        }
       if(stationclass.getNum()!=null){
           stationClassesSummary.setOrderNum(Long.valueOf(stationclass.getNum()));
       }
        if(stationclass.getPrintNum()!=null){
            stationClassesSummary.setPrintNum(stationclass.getPrintNum());
        }
        if(stationclass.getOrderLiters()!=null){
            stationClassesSummary.setSaleLiters(stationclass.getOrderLiters());
        }
        if(stationclass.getAmt()!=null){
            stationClassesSummary.setSaleAmt(stationclass.getAmt());
        }
        if(stationclass.getDzkAmt()!=null){
            stationClassesSummary.setDzkAmt(stationclass.getDzkAmt());
        }else{
            stationClassesSummary.setDzkAmt(BigDecimal.valueOf(0));
        }
        if(stationclass.getZfbAmt()!=null){
            stationClassesSummary.setZfbAmt(stationclass.getZfbAmt());
        }else{
            stationClassesSummary.setZfbAmt(BigDecimal.valueOf(0));
        }

        if(stationclass.getWxAmt()!=null){
            stationClassesSummary.setWxAmt(stationclass.getWxAmt());
        }else{
            stationClassesSummary.setWxAmt(BigDecimal.valueOf(0));
        }
        stationClassesSummary.setClassesMan(stationclass.getClassStructureMan());
        i=stationClassesSummaryService.insertStationClassesSummary(stationClassesSummary);

        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));
    }
}
