package com.xhpc.order.controller;

import com.xhpc.common.core.constant.HttpStatus;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.order.service.IXhpcPlaceholderOrderService;
import com.xhpc.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.Scope;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 占位费订单
 *
 * @author yuyang
 * @Date 2025-05-06 10:06
 */
@EnableScheduling
@RestController
@RequestMapping("/placeholderOrder")
@Api(value = "占位费订单", tags = "占位费订单")
public class XhpcPlaceholderOrderController extends BaseController {

        @Resource
        IXhpcPlaceholderOrderService xhpcPlaceholderOrderService;


        @GetMapping("/getListPage")
        public TableDataInfo getListPage(String tenantId, Long chargingStationId, Long chargingPileId, Long terminalId, String phone, String transactionNumber, @RequestParam("status")Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, @RequestParam("userId") Long userId, Integer type,
                                         String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult, Double totalMoneyMin, Double totalMoneyMax,String placeholderOrderNumber)

        {
                List<Map<String, Object>> listPage = xhpcPlaceholderOrderService.getListPage(tenantId,chargingStationId,chargingPileId,terminalId,phone, transactionNumber, status, chargingStationName, operatorId, source, beginStartTime, beginEndTime, userId, type, affiliationOrganization,evcsOrderNo,plateNum,internetId,internetSerialNumber,terminalName,vinCode,overStartTime,overEndTime,personnelId,confirmResult,totalMoneyMin,totalMoneyMax,placeholderOrderNumber);
                return getDataTable(listPage);
        }

        @GetMapping("/getAbnormalListPage")
        public TableDataInfo getAbnormalListPage(String tenantId, Long chargingStationId, Long chargingPileId, Long terminalId, String phone, String transactionNumber,Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, Long userId, Integer type,
                                         String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult)

        {
                List<Map<String, Object>> listPage = xhpcPlaceholderOrderService.getAbnormalListPage(tenantId,chargingStationId,chargingPileId,terminalId,phone, transactionNumber, status, chargingStationName, operatorId, source, beginStartTime, beginEndTime, userId, type, affiliationOrganization,evcsOrderNo,plateNum,internetId,internetSerialNumber,terminalName,vinCode,overStartTime,overEndTime,personnelId,confirmResult);
                return getDataTable(listPage);
        }
        /**
         * 历史订单导出
         * @param response 返回数据流
         * @throws IOException 文件创建异常
         */
        @PostMapping("/export")
        public void getExport(HttpServletResponse response,
                              String tenantId,
                              Long chargingStationId,
                              Long chargingPileId,
                              Long terminalId,
                              String phone,
                              String transactionNumber,
                              @RequestParam("status")Integer status,
                              String chargingStationName,
                              Long operatorId,
                              Integer source,
                              String beginStartTime,
                              String beginEndTime,
                              @RequestParam("userId") Long userId,
                              Integer type,
                              String affiliationOrganization,
                              String evcsOrderNo,
                              String plateNum,
                              Integer internetId,
                              String internetSerialNumber,
                              String terminalName,
                              String vinCode,
                              String overStartTime,
                              String overEndTime,
                              Long personnelId,
                              Integer confirmResult,
                              Double totalMoneyMin,
                              Double totalMoneyMax,
                              String placeholderOrderNumber) throws IOException {

                xhpcPlaceholderOrderService.export(response,tenantId,chargingStationId,chargingPileId,terminalId, phone, transactionNumber, status, chargingStationName, operatorId, source, beginStartTime, beginEndTime, userId, 1,affiliationOrganization,evcsOrderNo,plateNum,internetId,internetSerialNumber,terminalName,vinCode,overStartTime,overEndTime,personnelId,confirmResult, totalMoneyMin,totalMoneyMax,placeholderOrderNumber);
        }


        @GetMapping("/getById")
        public R getById(@RequestParam("placeholderOrderId")Long placeholderOrderId){
                return xhpcPlaceholderOrderService.getById(placeholderOrderId);
        }

        @GetMapping("/getxhpcPlaceholderOrderTime")
        public void getxhpcPlaceholderOrderTime(@RequestParam(value = "gunNumber")String gunNumber) {
              xhpcPlaceholderOrderService.getTimingPriceTime(gunNumber);
        }


        //归枪记录
        @GetMapping("/getReturnGunRecord")
        public void getReturnGunRecord(@RequestParam(value = "serialNumber")String serialNumber,@RequestParam(value = "startTime")String startTime,@RequestParam(value = "endTime")String endTime) {
                xhpcPlaceholderOrderService.getReturnGunRecord(serialNumber,startTime,endTime);
        }

        //超过360分钟的都变成异常订单
        //@Scheduled(cron = "0 0/5 * * * ? ")
        public void getAbnormal() {
                xhpcPlaceholderOrderService.getAbnormal();
        }

        //查询标记为异常状态手动处理的订单
        @Scheduled(cron = "0 0/3 * * * ? ")
        @GetMapping("/getType")
        public void getType() {
                xhpcPlaceholderOrderService.getType();
        }

        //查询实时占位费订单，是否存在下一个订单，存在该订单就异常
        @Scheduled(cron = "0 0/5 * * * ? ")
        @GetMapping("/getOrderNum")
        public void getOrderNum() {
                xhpcPlaceholderOrderService.getOrderNum();
        }



        @GetMapping("/getExamine")
        public AjaxResult getExamine(@RequestParam Long placeholderOrderId,BigDecimal totalMoney, @RequestParam String startTime , @RequestParam String endTime){
                return xhpcPlaceholderOrderService.getExamine(placeholderOrderId,totalMoney,startTime,endTime);
        }


        /**
         * 订单来源（第三方、支付宝、微信、运营商），权限划分
         * @param internetUserIds     订单来源集合
         * @return
         */
        @GetMapping("/getSumSourceList")
        public TableDataInfo getSumSourceList(HttpServletRequest request, String tenantId, Long operatorId, Long chargingStationId, Long chargingPileId, Long terminalId, String startTime, String endTime, String internetUserIds, String chargingStationIds) {
                startPage();
                List<Map<String, Object>> list = xhpcPlaceholderOrderService.getSumSourceList(request,tenantId,operatorId,chargingStationId,chargingPileId,terminalId,startTime,endTime,internetUserIds,chargingStationIds);
                return getDataTable(list);
        }

        /**
         * 订单来源统计导出
         * @return
         */
        @PostMapping("/internetUserInterval/export")
        public void internetUserIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId, String startTime, String endTime,String internetUserIds, String chargingStationIds) throws IOException {
                xhpcPlaceholderOrderService.getSumSourceListExport(request,response,tenantId,operatorId,chargingStationId,chargingPileId,terminalId, startTime,endTime,internetUserIds,chargingStationIds);
        }

        /**
         * 日期统计
         * @param chargingStationIds         电站id集合
         * @param internetUserId      流量方id
         * @param operatorId          运营商id
         * @param startTime           订单开始时间
         * @param endTime             订单结束时间
         * @param userId              用户id
         * @param type             1 平台 2 运营商 3流量方
         * @return
         */
        @GetMapping("/getDateIntervalPage")
        public TableDataInfo getDateIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime,Long userId, Integer type)
        {
                List<Map<String, Object>> list = xhpcPlaceholderOrderService.getDateIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
                return getDataTable(list);
        }

        /**
         * 日期统计导出
         * @param chargingStationIds         电站id集合
         * @param internetUserId      流量方id
         * @param operatorId          运营商id
         * @param startTime           订单开始时间
         * @param endTime             订单结束时间
         * @param userId              用户id
         * @param type             1 平台 2 运营商 3流量方
         * @return
         */
        @PostMapping("/dateInterval/export")
        public void dateIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime,Long userId,Integer type) throws IOException {
                xhpcPlaceholderOrderService.dateIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
        }

        /**
         * 电站统计
         * @param chargingStationIds  电站集合
         * @param operatorIds       运营商集合
         * @param internetUserId      流量方id
         * @param operatorId          运营商id
         * @param startTime           订单开始时间
         * @param endTime             订单结束时间
         * @param userId              用户id
         * @param type             1 平台 2 运营商 3流量方
         * @return
         */
        @GetMapping("/getStationIntervalPage")
        public TableDataInfo getStationIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds,String operatorIds,Long internetUserId, Long operatorId,String startTime, String endTime, Long userId, Integer type)
        {
                List<Map<String, Object>> list = xhpcPlaceholderOrderService.getStationIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,operatorIds,internetUserId,operatorId,startTime,endTime,userId,type);
                return getDataTable(list);
        }

        /**
         * 电站统计导出
         * @param chargingStationIds  电站集合
         * @param operatorIds       运营商集合
         * @param internetUserId      流量方id
         * @param operatorId          运营商id
         * @param startTime           订单开始时间
         * @param endTime             订单结束时间
         * @param userId              用户id
         * @param type             1 平台 2 运营商 3流量方
         * @return
         */
        @PostMapping("/stationInterval/export")
        public void stationIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds,String operatorIds,Long internetUserId, Long operatorId,String startTime, String endTime,Long userId,Integer type) throws IOException {
                xhpcPlaceholderOrderService.stationIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, chargingStationIds,operatorIds,internetUserId,operatorId,startTime,endTime,userId,type);
        }
        //==============================占位费查询================================

        @GetMapping("/getPlaceholderIDs")
        public void getPlaceholderIDs(@RequestParam(value = "stationIDs")String [] stationIDs) {
                xhpcPlaceholderOrderService.getPlaceholderIDs(stationIDs);
        }

        //==============================小程序接口================================
        @GetMapping("/appealPlaceholderOrderList")
        public TableDataInfo appealPlaceholderOrderList(HttpServletRequest request){
                List<Map<String, Object>> listPage =xhpcPlaceholderOrderService.appealPlaceholderOrderList(request);
                return getDataTable(listPage);
        }


        @PostMapping("/applyPlaceholderOrder")
        public R applyPlaceholderOrder(HttpServletRequest request,@RequestBody Map<String, Object> map) {
                if (map != null) {
                        Long placeholderOrderId = Long.parseLong(map.get("placeholderOrderId").toString());
                        String appealUserResult = map.get("appealUserResult").toString();
                        return xhpcPlaceholderOrderService.applyPlaceholderOrder(request,placeholderOrderId,appealUserResult);
                }
                return R.fail(HttpStatus.ERROR_STATUS, "修改失败");
        }

        //C端机构用户
        @PostMapping("/applyPlaceholderOrderProcess")
        public R applyPlaceholderOrderProcess(HttpServletRequest request,@RequestBody Map<String, Object> map) {
                if (map != null) {
                        Long placeholderOrderId = Long.parseLong(map.get("placeholderOrderId").toString());
                        String refundType = map.get("refundType").toString();
                        BigDecimal actPrice =new BigDecimal(0);
                        if(!"reject".equals(refundType)){

                                if("full".equals(refundType)){
                                        actPrice = new BigDecimal(map.get("placeholderTotalMoney").toString());
                                }else if("partial".equals(refundType)){
                                        actPrice = new BigDecimal(map.get("refundAmount").toString());
                                }
                        }

                        String appealResult = map.get("appealResult").toString();
                        String imgId = "";
                        if(map.get("imgId") !=null){
                                imgId = map.get("imgId").toString();
                        }

                        return xhpcPlaceholderOrderService.applyPlaceholderOrderProcess(request,placeholderOrderId,actPrice,refundType,appealResult,imgId);
                }
                return R.fail(HttpStatus.ERROR_STATUS, "修改失败");
        }

        //流量方
        @PostMapping("/applyPlaceholderOrderInternetProcess")
        public R applyPlaceholderOrderInternetProcess(HttpServletRequest request,@RequestBody Map<String, Object> map) {
                if (map != null) {
                        Long placeholderOrderId = Long.parseLong(map.get("placeholderOrderId").toString());
                        String refundType = map.get("refundType").toString();
                        BigDecimal actPrice =new BigDecimal(0);
                        if(!"reject".equals(refundType)){

                                if("full".equals(refundType)){
                                        actPrice = new BigDecimal(map.get("totalMoney").toString());
                                }else if("partial".equals(refundType)){
                                        actPrice = new BigDecimal(map.get("refundAmount").toString());
                                }
                        }

                        String appealResult = map.get("appealResult").toString();
                        String ingId = "";
                        if(map.get("ingId") !=null){
                                ingId = map.get("ingId").toString();
                        }
                        return xhpcPlaceholderOrderService.applyPlaceholderOrderInternetProcess(request,placeholderOrderId,actPrice,refundType,appealResult,ingId);
                }
                return R.fail(HttpStatus.ERROR_STATUS, "修改失败");
        }



        @GetMapping("/appealPlaceholderOrderListPC")
        public TableDataInfo appealPlaceholderOrderListPC(HttpServletRequest request,String tenantId,Long operatorId,String name, Integer type, String serialNumber, Long chargingStationId){
                List<Map<String, Object>> listPage =xhpcPlaceholderOrderService.appealPlaceholderOrderListPC(request,tenantId,operatorId,name, type, serialNumber, chargingStationId);
                return getDataTable(listPage);
        }


        @Log(title = "上传对账单(文件方式)", businessType = BusinessType.INSERT)
        @PostMapping("/file/upload")
        public R fileUpload(HttpServletRequest request, HttpServletResponse response, MultipartFile file, int type, String startTime, String endTime) throws Exception {
                if (file ==null || type < 1){
                        return R.fail("文件或者对账类型为空");
                }
                return xhpcPlaceholderOrderService.fileUpload(request,response,"000000", file, type, startTime, endTime);
        }

        @GetMapping("/deviceMessageList")
        public TableDataInfo deviceMessageList(@RequestParam String serialNumber)
        {
                List<Map<String,Object>> list = xhpcPlaceholderOrderService.deviceMessageList(serialNumber);
                return getDataTable(list);
        }
}
