package com.platform.yijia.controller;
import com.google.gson.Gson;
import com.platform.yijia.param.request.LoginBody;
import com.platform.yijia.pojo.PayOrder;
import com.platform.yijia.pojo.StationClassStructure;
import com.platform.yijia.pojo.StationClassesSummary;
import com.platform.yijia.pojo.SysUser;
import com.platform.yijia.service.IStationClassStructureService;
import com.platform.yijia.service.IStationClassesSummaryService;
import com.platform.yijia.service.PayOrderService;
import com.platform.yijia.service.PosMachineService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
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
@RequestMapping("/demo")
public class PosMachineController {
    @Resource
    private PosMachineService posMachineService;

    @Resource
    private IStationClassStructureService stationClassStructureService;

    @Resource
    private IStationClassesSummaryService stationClassesSummaryService;

    @Resource
    private PayOrderService payOrderService;

    //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();
                    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
                    //密码加密判断已加密密码是否与真实密码相同
                    boolean matches = bCryptPasswordEncoder.matches(loginBody.getPassword(), 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);
    }


    /*
     * 查询订单列表
     * @param stationId
     * @param userName
     * @return
     */
    @RequestMapping (value = "/selectPosPayOrdersList", method = RequestMethod.GET)
    @ResponseBody
    public String selectPosPayOrdersList(@RequestParam Integer stationId, String beginTime, String endTime){
        Gson gson =new Gson();
        //返回结果集
        ResultData resultData = null;
        PayOrder payOrder = new PayOrder();
        payOrder.setStationId(stationId);
        payOrder.setBeginTime(beginTime);
        payOrder.setEndTime(endTime);
        payOrder.setStatus("1");
        List<PayOrder> payOrders = payOrderService.selectPayOrderList(payOrder);
        resultData = ResultData.success(payOrders);
        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
    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.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.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());
            }
            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);
            if(i==1){
                map.put("code","200");
                map.put("msg","班结成功");
//                //查询班结小票数据
//                StationClassStructure classStructure= new StationClassStructure();
//                classStructure.setClassStructureNo(no);
//                classStructure.setStationId(stationId);
//                //根据油品汇总数据
//                List<StationClassStructure> list= stationClassStructureService.listOilStructure(classStructure);
//                if(list!=null && list.size()>0){
//                    map.put("code","200");
//                    map.put("msg","班结成功");
//                    map.put("oilList",list);
//                }
            }
        }else{
            map.put("code","100");
            map.put("msg","已班结");
        }
        return map;
    }



}
