package com.platform.yijia.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.platform.yijia.config.YiJiaRequest;
import com.platform.yijia.param.request.LoginBody;
import com.platform.yijia.pojo.*;
import com.platform.yijia.service.*;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.CryptUtil;
import com.platform.yijia.utils.DESUtils;
import com.platform.yijia.utils.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.ObjectHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <Title> PosMachineController </Title>
 * <Description> POS机端模块功能 </Description>
 * @Author JK
 * @Date 2021年3月17日
 */

@Controller
@YiJiaRequest()
@CrossOrigin
public class PosMachineController {
    @Resource
    private PosMachineService posMachineService;

    @Resource
    private StationService stationService;

    @Resource
    private IStationClassStructureService stationClassStructureService;

    @Resource
    private IStationClassesSummaryService stationClassesSummaryService;

    @Resource
    private PayOrderService payOrderService;

    @Value("${3des.pwd}")
    private String pwd;

    //POS机端用户登录
    @RequestMapping(value = "/loginPosMachine", method = RequestMethod.POST)
    @ResponseBody
    public String loginPosMachine(@RequestBody LoginBody loginBody){

        //Using generated security password: a78f4bd0-6087-4fff-ae5d-f2e4cd8c7961
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(loginBody.getUserName() !=null && loginBody.getPassword() !=null){
            SysUser sysUser = posMachineService.selectUserByUserName(loginBody.getUserName());
            if(sysUser !=null){
                if(sysUser.getStatus().equals("0")){
                    String password = sysUser.getPassword();
                    //password = CryptUtil.desedeDecrypt(password);
                    String webPassword = loginBody.getPassword();   //前端传来加密码
                    String ps= DESUtils.decrypt(webPassword, pwd);
                    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
                    //密码加密判断已加密密码是否与真实密码相同
                    boolean matches = bCryptPasswordEncoder.matches(ps, password);
                    if(matches){
                        SysUser s = new SysUser();
                        s.setNickName(sysUser.getNickName());
                        s.setDeptId(sysUser.getDeptId());
                        s.setAvatar(sysUser.getAvatar());
                        s.setUserName(sysUser.getUserName());
                        s.setPhonenumber(sysUser.getPhonenumber());
                        s.setSex(sysUser.getSex());
                        s.setUserId(sysUser.getUserId());
                        s.setDeptId(sysUser.getDeptId());
                        resultData = ResultData.success(s);
                    }else{
                        resultData = ResultData.error(CodeMsg.PASSWORD_ERROR);
                    }
                }else if(sysUser.getStatus().equals("1")){
                    resultData = ResultData.error(CodeMsg.ACCOUNT_DISABLE);
                }
            }else {
                resultData = ResultData.error(CodeMsg.USER_NOT_EXSIST);
            }
        }
        return gson.toJson(resultData);
    }

    //获取该油站油枪绑定的POS机信息
    @RequestMapping (value = "/getStationDeviceManager", method = RequestMethod.GET)
    @ResponseBody
    public String getStationDeviceManager(@RequestParam Integer stationId, String oilGun, String deviceType){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(stationId !=null  && StringUtils.isNotBlank(deviceType)){
            StationDeviceManager stationDeviceManager = new StationDeviceManager();
            stationDeviceManager.setGunNo(oilGun);
            stationDeviceManager.setStationId(stationId);
            stationDeviceManager.setDeviceType(deviceType);
            //获取油枪绑定的打印设备信息
            StationDeviceManager stationDeviceManagerList = stationService.getStationDeviceManager(stationDeviceManager);
            resultData = ResultData.success(stationDeviceManagerList);
        }else {
            resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }

    //根据POS机tusn获取该油站油枪绑定的POS机信息
    @RequestMapping (value = "/getStationDeviceManagerBytusn", method = RequestMethod.GET)
    @ResponseBody
    public String getStationDeviceManagerBytusn(@RequestParam String tusn){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(StringUtils.isNotBlank(tusn)){
            StationDeviceManager stationDeviceManager = new StationDeviceManager();
            stationDeviceManager.setDeviceNo(tusn);
            //获取油枪绑定的打印设备信息
            StationDeviceManager stationDeviceManagerList = stationService.getStationDeviceManager(stationDeviceManager);
            resultData = ResultData.success(stationDeviceManagerList);
        }else {
            resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }



    //查询POS机昨天和近七天数据
    @RequestMapping (value = "/selectPosYesterDayAndSevenData", method = RequestMethod.GET)
    @ResponseBody
    public String selectPosYesterDayAndSevenData(@RequestParam Integer stationId){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(stationId !=null){
            PayOrder payOrder = new PayOrder();
            payOrder.setStatus("1");
            payOrder.setStationId(stationId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            payOrder.setEndTime(sdf.format(new Date()));
            Calendar c = new GregorianCalendar();
            c.setTime(new Date());
            c.add(Calendar.DATE, -8);
            payOrder.setBeginTime(sdf.format(c.getTime()));
            //获取近七天数据
            List<Map<String, Object>> mapList = payOrderService.selectPosRecentlySevenData(payOrder);
            //获取昨日数据
            Calendar cc = new GregorianCalendar();
            payOrder.setEndTime(sdf.format(new Date()));
            cc.add(Calendar.DATE, -1);
            payOrder.setBeginTime(sdf.format(cc.getTime()));
            cc.add(Calendar.DATE, -6);
            payOrder.setEndTimeQOQ(sdf.format(cc.getTime()));
            cc.add(Calendar.DATE, -1);
            payOrder.setBeginTimeQOQ(sdf.format(cc.getTime()));
            Map<String, Object> map = payOrderService.selectPosYesterDayData(payOrder);
            //存放结果集
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("yesterDayData", map);
            resultMap.put("recentlySevenData", mapList);
            resultData = ResultData.success(resultMap);
        }else {
            resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
        }
        return gson.toJson(resultData);
    }


    // 获取订单的支付方式列表
    @RequestMapping (value = "/selectPayTypeList")
    @ResponseBody
    public String selectPayTypeList(){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        List<PayType> payTypeList = payOrderService.selectPayTypeList();
        Map<String, Object> map = new HashMap<>();
        map.put("payTypeList", payTypeList);
        resultData = ResultData.success(map);
        return  gson.toJson(resultData);
    }

    /*
     * 查询订单列表
     * @param stationId
     * @param userName
     * @return
     */
    @RequestMapping (value = "/selectPosPayOrdersList", method = RequestMethod.POST)
    @ResponseBody
    public String selectPosPayOrdersList(@RequestBody PayOrder request){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(request.getStationId() !=null){
            PayOrder payOrder = new PayOrder();
            payOrder.setStationId(request.getStationId());
            payOrder.setBeginTime(request.getBeginTime());
            payOrder.setEndTime(request.getEndTime());
            payOrder.setPayType(request.getPayType());
            payOrder.setStatus("1");
            Map<String, Object> resultMap = new HashMap<>();
            //分页
            if(request.getPageNum() !=null && request.getPageSize() !=null){
                PageHelper.startPage(request.getPageNum(), request.getPageSize());
                List<PayOrder> payOrderList = payOrderService.selectPayOrderListFormater(payOrder);
                PageInfo pageInfo = new PageInfo(payOrderList);
                resultMap.put("orderList", pageInfo);
                resultData = ResultData.success(resultMap);
            }else {
                resultData = ResultData.error(CodeMsg.PARAMS_NUll);
            }
        }else {
            resultData = ResultData.error(CodeMsg.PARAMS_NUll);
        }
        return  gson.toJson(resultData);
    }

    /*
     * 查询班结后总数据
     * @param stationId
     * @param userName
     * @return
     */
    @RequestMapping (value = "/getStationClassesSummaryList", method = RequestMethod.GET)
    @ResponseBody
    public String getStationClassesSummaryList(@RequestParam String classStructureTime, Integer stationId) throws ParseException {
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(stationId !=null){
            StationClassesSummary stationClassesSummary = new StationClassesSummary();
            stationClassesSummary.setStationId(stationId);
            if(classStructureTime !=null && classStructureTime !=""){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                stationClassesSummary.setStartDate(sdf.parse(classStructureTime));
                Calendar c = Calendar.getInstance();
                c.setTime(sdf.parse(classStructureTime));
                c.add(Calendar.DAY_OF_MONTH,+1);
                stationClassesSummary.setEndDate(c.getTime());
            }
            List<Map> stationClassesSummariesList = stationClassesSummaryService.getStationClassesSummaryList(stationClassesSummary);
            Map map = new HashMap();
            map.put("listSum", stationClassesSummariesList);
            resultData = ResultData.success(map);
        }else {
            resultData = ResultData.success(CodeMsg.REQUEST_FAIL);
        }

        return  gson.toJson(resultData);
    }

    /*
     * 获取班结人要班结的订单总和信息
     * @param stationId
     * @param userName
     * @return
     */
    @RequestMapping (value = "/getClassStructureListByClassStructureNo", method = RequestMethod.GET)
    @ResponseBody
    public String getClassStructureListByClassStructureNo(@RequestParam Integer stationId, String classStructureNo){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(stationId !=null && classStructureNo !=null){
            StationClassStructure classes =new StationClassStructure();
            classes.setStationId(stationId);
            classes.setClassStructureNo(classStructureNo);
            classes.setGroupBy("");
            List<Map<String, String>> listSum = stationClassStructureService.selectClassStructureSum(classes);
            classes.setGroupBy("oilName");
            List<Map<String, String>> listSumGroupByOilName = stationClassStructureService.selectClassStructureSum(classes);
            classes.setGroupBy("payType");
            List<Map<String, String>> listSumGroupByPayType = stationClassStructureService.selectClassStructureSum(classes);
            classes.setGroupBy("timeAndMan");
            List<Map<String, String>> timeList = stationClassStructureService.selectClassStructureSum(classes);
//            if(timeList !=null && listSum.size() ==0){
//                //第一次班结
//                if(timeList.get(0).get("beginTime").equals("")){
//                    PayOrder payOrder = new PayOrder();
//                    payOrder.setStatus("1");
//                    payOrder.setStationId(stationId);
//                    PayOrder payOrderInfoMin = payOrderService.getPayOrderInfoMin(payOrder);
//                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    //没有班结过：第一次班结
//                    timeList.get(0).replace("beginTime", dateFormat.format(payOrderInfoMin.getPayDate()));
//                }
//            }
            Map<String, Object> map = new HashMap();
            map.put("listSumGroupByOilName", listSumGroupByOilName);
            map.put("listSum", listSum);
            map.put("timeList", timeList);
            map.put("listSumGroupByPayType", listSumGroupByPayType);
            resultData = ResultData.success(map);
        }
        return gson.toJson(resultData);
    }


    /*
     * 获取班结人要班结的订单总和信息
     * @param stationId
     * @param userName
     * @return
     */
    @RequestMapping (value = "/getClassStructureList", method = RequestMethod.GET)
    @ResponseBody
    public String getClassStructureList(@RequestParam Integer stationId) throws ParseException {
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        if(stationId !=null){
            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 dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(sss != null){
                //班结过
                beginTime= dateFormat.format(sss.getClassStructureDate());
            }else {
                PayOrder payOrder = new PayOrder();
                payOrder.setStatus("1");
                payOrder.setStationId(stationId);
                PayOrder payOrderInfoMin = payOrderService.getPayOrderInfoMin(payOrder);
                //没有班结过：第一次班结
                beginTime =dateFormat.format(payOrderInfoMin.getPayDate());
            }
            //获取上次班结时间
            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            order.setStatus("1");
            order.setBeginTime(beginTime);
            order.setEndTime(dateFormat.format(date));
            //查询要班结订单总和
            order.setGroupBy("");
            List<Map> listSum = payOrderService.selectSum(order);

            //班结时间
            List<Map> timeList = new ArrayList<>();
            Map<String, String> m = new HashMap<>();
            m.put("beginTime", beginTime);
            m.put("endTime", dateFormat.format(date));
            timeList.add(m);

            //查询要班结订单总和按油品分类
            order.setGroupBy("oilName");
            List<Map> listSumGroupByOilName = payOrderService.selectSum(order);
            //查询要班结订单总和按支付方式
            order.setGroupBy("payType");
            List<Map> listSumGroupByPayType = payOrderService.selectSum(order);
            Map<String, Object> map = new HashMap();
            map.put("listSumGroupByOilName", listSumGroupByOilName);
            map.put("listSum", listSum);
            map.put("timeList", timeList);
            map.put("listSumGroupByPayType", listSumGroupByPayType);
            resultData =ResultData.success(map);
        }else {
            resultData =ResultData.error(CodeMsg.PARAMS_NUll);
        }
        return  gson.toJson(resultData);
    }




    /*
     * pos机班结
     * @param stationClassStructure
     * @return
     * @throws ParseException
     */
    @RequestMapping (value = "/posClassStructure", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public Map posClassStructure (@RequestBody StationClassStructure stationClassStructure) throws ParseException {
        Map map = new HashMap();
        //获取当前班结油站
        Integer stationId=stationClassStructure.getStationId();
        //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;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(sss!=null){
            //获取上次班结时间
            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            order.setStatus("1");
            beginTime= dateFormat.format(sss.getClassStructureDate());
            order.setBeginTime(beginTime);
            order.setEndTime( dateFormat.format(date));
            List<PayOrder> list = payOrderService.selectPayOrderList(order);
            for(PayOrder payOrder:list){
                StationClassStructure stationclass =new StationClassStructure();
                stationclass.setStationId(payOrder.getStationId());
                stationclass.setStationName(payOrder.getStationName());
                stationclass.setClassStartDate(dateFormat.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(new BigDecimal(payOrder.getAmt()));
                if(payOrder.getReceivableAmt()!=null){
                    stationclass.setReceivableAmt(new BigDecimal(payOrder.getReceivableAmt()));
                }else{
                    stationclass.setReceivableAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getReceivedAmt()!=null){
                    stationclass.setReceivedAmt(new BigDecimal(payOrder.getReceivedAmt()));
                }else{
                    stationclass.setReceivedAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDiscountAmt()!=null){
                    stationclass.setDiscountAmt(new BigDecimal(payOrder.getDiscountAmt()));
                }else{
                    stationclass.setDiscountAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDiscountCouponAmt()!=null){
                    stationclass.setDiscountCouponAmt(new BigDecimal(payOrder.getDiscountCouponAmt()));
                }else{
                    stationclass.setDiscountCouponAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getWxAmt()!=null){
                    stationclass.setWxAmt(new BigDecimal(payOrder.getWxAmt()));
                }else{
                    stationclass.setWxAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getZfbAmt()!=null){
                    stationclass.setZfbAmt(new BigDecimal(payOrder.getZfbAmt()));
                }else{
                    stationclass.setZfbAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getPosAmt()!=null){
                    stationclass.setPosAmt(new BigDecimal(payOrder.getPosAmt()));
                }else{
                    stationclass.setPosAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getXjAmt()!=null){
                    stationclass.setXjAmt(new BigDecimal(payOrder.getXjAmt()));
                }else{
                    stationclass.setXjAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDidiAppAmt()!=null){
                    stationclass.setDidiAppAmt(new BigDecimal(payOrder.getDidiAppAmt()));
                }else{
                    stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getTyAppAmt()!=null){
                    stationclass.setDidiAppAmt(new BigDecimal(payOrder.getTyAppAmt()));
                }else{
                    stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getOtherAmt()!=null){
                    stationclass.setOtherAmt(new BigDecimal(payOrder.getOtherAmt()));
                }else{
                    stationclass.setOtherAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDzkAmt()!=null){
                    stationclass.setDzkAmt(new BigDecimal(payOrder.getDzkAmt()));
                }else{
                    stationclass.setDzkAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getMemberAmt()!=null){
                    stationclass.setMemberAmt(new BigDecimal(payOrder.getMemberAmt()));
                }else{
                    stationclass.setMemberAmt(BigDecimal.valueOf(0));
                }
                stationclass.setOrderLiters(new BigDecimal(payOrder.getOrderLiters()));
                stationclass.setPayWay(payOrder.getPayWay());
                stationclass.setPayType(payOrder.getPayType());
                stationclass.setPayDate(payOrder.getPayDate());
                stationclass.setOilPersonnel(payOrder.getOilPersonnel());
                stationclass.setOrderType(payOrder.getOrderType());
                if(payOrder.getPrintCount()!=null){
                    stationclass.setPrintNum(payOrder.getPrintCount());
                }else{
                    stationclass.setPrintNum(Integer.valueOf("0"));
                }
                i= stationClassStructureService.insertStationClassStructure(stationclass);
            }
        }else{
            //初次班结 班结时间默认为当前时间，班结人username
            //根据班结时间查询订单
            PayOrder order=new PayOrder();
            order.setStationId(stationId);
            order.setStatus("1");
            //获取第一个最早时间的订单
            PayOrder payOrderInfoMin = payOrderService.getPayOrderInfoMin(order);
            //没有班结过：第一次班结获取第一个订单的下单时间
            beginTime =dateFormat.format(payOrderInfoMin.getPayDate());
            order.setBeginTime(beginTime);
            order.setEndTime(dateFormat.format(date));

            List<PayOrder> list = payOrderService.selectPayOrderList(order);
            for(PayOrder payOrder:list){
                StationClassStructure stationclass =new StationClassStructure();
                stationclass.setClassStartDate(payOrderInfoMin.getPayDate());
                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(new BigDecimal(payOrder.getAmt()));
                if(payOrder.getReceivableAmt()!=null){
                    stationclass.setReceivableAmt(new BigDecimal(payOrder.getReceivableAmt()));
                }else{
                    stationclass.setReceivableAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getReceivedAmt()!=null){
                    stationclass.setReceivedAmt(new BigDecimal(payOrder.getReceivedAmt()));
                }else{
                    stationclass.setReceivedAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDiscountAmt()!=null){
                    stationclass.setDiscountAmt(new BigDecimal(payOrder.getDiscountAmt()));
                }else{
                    stationclass.setDiscountAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDiscountCouponAmt()!=null){
                    stationclass.setDiscountCouponAmt(new BigDecimal(payOrder.getDiscountCouponAmt()));
                }else{
                    stationclass.setDiscountCouponAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getWxAmt()!=null){
                    stationclass.setWxAmt(new BigDecimal(payOrder.getWxAmt()));
                }else{
                    stationclass.setWxAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getZfbAmt()!=null){
                    stationclass.setZfbAmt(new BigDecimal(payOrder.getZfbAmt()));
                }else{
                    stationclass.setZfbAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getPosAmt()!=null){
                    stationclass.setPosAmt(new BigDecimal(payOrder.getPosAmt()));
                }else{
                    stationclass.setPosAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getXjAmt()!=null){
                    stationclass.setXjAmt(new BigDecimal(payOrder.getXjAmt()));
                }else{
                    stationclass.setXjAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDidiAppAmt()!=null){
                    stationclass.setDidiAppAmt(new BigDecimal(payOrder.getDidiAppAmt()));
                }else{
                    stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getTyAppAmt()!=null){
                    stationclass.setDidiAppAmt(new BigDecimal(payOrder.getTyAppAmt()));
                }else{
                    stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getOtherAmt()!=null){
                    stationclass.setOtherAmt(new BigDecimal(payOrder.getOtherAmt()));
                }else{
                    stationclass.setOtherAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getDzkAmt()!=null){
                    stationclass.setDzkAmt(new BigDecimal(payOrder.getDzkAmt()));
                }else{
                    stationclass.setDzkAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getMemberAmt()!=null){
                    stationclass.setMemberAmt(new BigDecimal(payOrder.getMemberAmt()));
                }else{
                    stationclass.setMemberAmt(BigDecimal.valueOf(0));
                }
                if(payOrder.getOrderLiters() !=null){
                    stationclass.setOrderLiters(new BigDecimal(payOrder.getOrderLiters()));
                }else {
                    stationclass.setOrderLiters(new BigDecimal(0));
                }
                stationclass.setPayWay(payOrder.getPayWay());
                stationclass.setPayType(payOrder.getPayType());
                stationclass.setPayDate(payOrder.getPayDate());
                stationclass.setOilPersonnel(payOrder.getOilPersonnel());
                stationclass.setOrderType(payOrder.getOrderType());
                if(payOrder.getPrintCount()!=null){
                    stationclass.setPrintNum(payOrder.getPrintCount());
                }else{
                    stationclass.setPrintNum(Integer.valueOf("0"));
                }
                i= stationClassStructureService.insertStationClassStructure(stationclass);
            }
        }
        StationClassStructure scs =new StationClassStructure();
        scs.setClassStructureNo(no);
        //汇总班结数据将汇总的数据插入到汇总数据中
        StationClassStructure stationclass = stationClassStructureService.selectSummyClassStructure(scs);
        if(stationclass!=null){
            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(Integer.valueOf(stationclass.getNum()));
            }
            if(stationclass.getPrintNum()!=null){
                stationClassesSummary.setPrintNum(stationclass.getPrintNum());
            }else {
                stationClassesSummary.setPrintNum(Integer.valueOf(0));
            }
            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));
            }
            if(stationclass.getAmt() !=null){
                stationClassesSummary.setAmt(stationclass.getAmt());
            }else {
                stationClassesSummary.setAmt(BigDecimal.valueOf(0));
            }
            if(stationclass.getDiscountAmt() !=null){
                stationClassesSummary.setDiscountAmt(stationclass.getDiscountAmt());
            }else {
                stationClassesSummary.setDiscountAmt(BigDecimal.valueOf(0));
            }
            stationClassesSummary.setClassesMan(stationclass.getClassStructureMan());
            i=stationClassesSummaryService.insertStationClassesSummary(stationClassesSummary);
            if(i==1){
                map.put("retCode","0");
                map.put("msg","班结成功");
            }
        }else{
            map.put("retCode","-1");
            map.put("msg","已班结");
        }
        return map;
    }


    /*
     * pos机班结
     * @param stationClassStructure
     * @return
     * @throws ParseException
     */
    @RequestMapping (value = "/posClassStructureNew", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public Map posClassStructureNew (@RequestBody StationClassStructure stationClassStructure) throws ParseException {
        Map map = new HashMap();
        //获取当前班结油站
        Integer stationId=stationClassStructure.getStationId();
        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;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PayOrder order=new PayOrder();
        order.setStationId(stationId);
        order.setStatus("1");
        PayOrder payOrderInfoMin = new PayOrder();
        if(sss!=null){
            //获取上次班结时间
            beginTime= dateFormat.format(sss.getClassStructureDate());
            order.setBeginTime(beginTime);
            order.setEndTime( dateFormat.format(date));
        }else{
            //获取第一个最早时间的订单
            payOrderInfoMin = payOrderService.getPayOrderInfoMin(order);
            //初次班结 班结时间默认为当前时间，班结人username
            //根据班结时间查询订单
            //没有班结过：第一次班结获取第一个订单的下单时间
            beginTime =dateFormat.format(payOrderInfoMin.getPayDate());
            order.setBeginTime(beginTime);
            order.setEndTime(dateFormat.format(date));
        }
        List<PayOrder> orderList = payOrderService.selectPayOrderList(order);
        //批量插入要班结的订单
        List<StationClassStructure>  stationClassStructureList= new ArrayList<>();
        for (PayOrder  payOrder: orderList){
            StationClassStructure stationclass =new StationClassStructure();
            stationclass.setClassStartDate(payOrderInfoMin.getPayDate());
            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(new BigDecimal(payOrder.getAmt()));
            if(payOrder.getReceivableAmt()!=null){
                stationclass.setReceivableAmt(new BigDecimal(payOrder.getReceivableAmt()));
            }else{
                stationclass.setReceivableAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getReceivedAmt()!=null){
                stationclass.setReceivedAmt(new BigDecimal(payOrder.getReceivedAmt()));
            }else{
                stationclass.setReceivedAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getDiscountAmt()!=null){
                stationclass.setDiscountAmt(new BigDecimal(payOrder.getDiscountAmt()));
            }else{
                stationclass.setDiscountAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getDiscountCouponAmt()!=null){
                stationclass.setDiscountCouponAmt(new BigDecimal(payOrder.getDiscountCouponAmt()));
            }else{
                stationclass.setDiscountCouponAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getWxAmt()!=null){
                stationclass.setWxAmt(new BigDecimal(payOrder.getWxAmt()));
            }else{
                stationclass.setWxAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getZfbAmt()!=null){
                stationclass.setZfbAmt(new BigDecimal(payOrder.getZfbAmt()));
            }else{
                stationclass.setZfbAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getPosAmt()!=null){
                stationclass.setPosAmt(new BigDecimal(payOrder.getPosAmt()));
            }else{
                stationclass.setPosAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getXjAmt()!=null){
                stationclass.setXjAmt(new BigDecimal(payOrder.getXjAmt()));
            }else{
                stationclass.setXjAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getDidiAppAmt()!=null){
                stationclass.setDidiAppAmt(new BigDecimal(payOrder.getDidiAppAmt()));
            }else{
                stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getTyAppAmt()!=null){
                stationclass.setDidiAppAmt(new BigDecimal(payOrder.getTyAppAmt()));
            }else{
                stationclass.setDidiAppAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getOtherAmt()!=null){
                stationclass.setOtherAmt(new BigDecimal(payOrder.getOtherAmt()));
            }else{
                stationclass.setOtherAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getDzkAmt()!=null){
                stationclass.setDzkAmt(new BigDecimal(payOrder.getDzkAmt()));
            }else{
                stationclass.setDzkAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getMemberAmt()!=null){
                stationclass.setMemberAmt(new BigDecimal(payOrder.getMemberAmt()));
            }else{
                stationclass.setMemberAmt(BigDecimal.valueOf(0));
            }
            if(payOrder.getOrderLiters() !=null){
                stationclass.setOrderLiters(new BigDecimal(payOrder.getOrderLiters()));
            }else {
                stationclass.setOrderLiters(new BigDecimal(0));
            }
            stationclass.setPayWay(payOrder.getPayWay());
            stationclass.setPayType(payOrder.getPayType());
            stationclass.setPayDate(payOrder.getPayDate());
            stationclass.setOilPersonnel(payOrder.getOilPersonnel());
            stationclass.setOrderType(payOrder.getOrderType());
            if(payOrder.getPrintCount()!=null){
                stationclass.setPrintNum(payOrder.getPrintCount());
            }else{
                stationclass.setPrintNum(Integer.valueOf("0"));
            }
            stationClassStructureList.add(stationclass);
        }
        int insertSize = stationClassStructureService.insertStationClassStructureBatch(stationClassStructureList);
        if(insertSize == orderList.size()){
            StationClassStructure scs =new StationClassStructure();
            scs.setClassStructureNo(no);
            //汇总班结数据将汇总的数据插入到汇总数据中
            StationClassStructure stationclass = stationClassStructureService.selectSummyClassStructure(scs);
            if(stationclass!=null){
                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(Integer.valueOf(stationclass.getNum()));
                }
                if(stationclass.getPrintNum()!=null){
                    stationClassesSummary.setPrintNum(stationclass.getPrintNum());
                }else {
                    stationClassesSummary.setPrintNum(Integer.valueOf(0));
                }
                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));
                }
                if(stationclass.getAmt() !=null){
                    stationClassesSummary.setAmt(stationclass.getAmt());
                }else {
                    stationClassesSummary.setAmt(BigDecimal.valueOf(0));
                }
                if(stationclass.getDiscountAmt() !=null){
                    stationClassesSummary.setDiscountAmt(stationclass.getDiscountAmt());
                }else {
                    stationClassesSummary.setDiscountAmt(BigDecimal.valueOf(0));
                }
                stationClassesSummary.setClassesMan(stationclass.getClassStructureMan());
                i=stationClassesSummaryService.insertStationClassesSummary(stationClassesSummary);
                if(i==1){
                    map.put("retCode","0");
                    map.put("msg","班结成功");
                }
            }else{
                map.put("retCode","-1");
                map.put("msg","已班结");
            }
        }else {
            map.put("retCode","-2");
            map.put("msg","班结失败");
        }
        return map;
    }

}
