package com.steerinfo.appservice.apppretrackscale.controller;

import com.steerinfo.appservice.apppretrackscale.model.AppPreTrackScale;
import com.steerinfo.appservice.apppretrackscale.service.IAppPreTrackScaleService;
import com.steerinfo.appservice.dto.AppPreTrackScaleDto;
import com.steerinfo.feign.DilFeign;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.pretrack.pretracknotice.model.PreTrackNotice;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.pretrack.pretrackscale.service.IPreTrackScaleService;
import com.steerinfo.util.AESUtil;
import com.steerinfo.util.FieldsCollector;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * PreTrackScale RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2020-10-20 09:26
 * 类描述
 * 修订历史：
 * 日期：2020-10-20
 * 作者：generator
 * 参考：
 * 描述：PreTrackScale RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/appservice/pretrackscales")
public class AppPreTrackScaleController extends BaseRESTfulController {

    @Autowired
    IAppPreTrackScaleService apppreTrackScaleService;

    @Autowired
    IPreTrackScaleService preTrackScaleService;

    @Autowired
    DilFeign dilFeignService;

    @ApiOperation(value = "App端获取预报信息", notes = "通过供应商获取预报信息")
    @GetMapping(value = "/doQueryApp")
    public RESTfulResult doQueryApp(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<AppPreTrackScale> list = apppreTrackScaleService.doQueryApp(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "App端获取预报信息", notes = "通过车号获取预报信息")
    @GetMapping(value = "/doQueryAppByCar")
    public RESTfulResult doQueryAppByCar(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            PageList<AppPreTrackScale> list = apppreTrackScaleService.doQueryAppByCar(parmas, pageNum, pageSize);
            return success(list);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "二维码扫码解密", notes = "通过车号获取预报信息")
    @PostMapping(value = "scanAndDecrypt")
    public RESTfulResult scanAndDecrypt(@RequestBody String str) {
        try {
            RESTfulResult rm = success();
            String deStr = AESUtil.decrypt(str, "abcdefghijklmnop", "0000000000000000");
            String[] strs = deStr.split(",");
            if (strs[2].equals("1")) {
                MeterWorkCarActualFirst model = apppreTrackScaleService.scanAndDecryptFrist(strs[0], strs[1]);
                rm.setCode("200");
                rm.setMessage("1");
                rm.setData(model);
            }
            if (strs[2].equals("2")) {
                MeterWorkCarActual model = apppreTrackScaleService.scanAndDecryptActual(strs[0], strs[1]);
                rm.setCode("200");
                rm.setMessage("2");
                rm.setData(model);
            }
            return rm;
        } catch (Exception e) {
            return failed(500, "操作异常，请确认");
        }
    }


    @ApiOperation(value = "新增数据", notes = "根据PreTrackScale对象创建")
    @PostMapping(value = "/add")
    public RESTfulResult addByNotice(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            PreTrackScale pts = apppreTrackScaleService.addByNotice(model);
            if (pts != null) {
                return success(pts);
            } else {
                return failed("新增失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }


    @ApiOperation(value = "通过接口新增计量预报", notes = "根据AppPreTrackScaleDto对象创建")
    @PostMapping(value = "/addFroInterface")
    public RESTfulResult addFroInterface(@RequestBody AppPreTrackScaleDto model) {
        RESTfulResult rm = failed();
        try {
            PreTrackScale pts;
            model = apppreTrackScaleService.addBaseFroInterface(model);
            pts = apppreTrackScaleService.addFroInterface(model);
//            dilFeignService.delWaybillAndPlanByPreTrackScale(FieldsCollector.beanToMap(model));
            if (pts != null) {
                return success(pts);
            } else {
                return failed("新增失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
            rm.setFailedMessage(e.getMessage());
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "App端预约配车", notes = "分页查询")
    @PostMapping(value = "/allocationApp", produces = "application/json;charset=UTF-8")
    public RESTfulResult allocationApp(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            PreTrackScale pts = apppreTrackScaleService.allocationApp(model);
            return success(pts);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "App端获取合同信息", notes = "通过供应商获取预报信息")
    @GetMapping(value = "/doQueryNoticeApp")
    public RESTfulResult doQueryNoticeApp(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<PreTrackNotice> list = apppreTrackScaleService.doQueryNoticeApp(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value = "停用数据", notes = "根据PreTrackScale对象停用")
    @ApiImplicitParam(name = "model", value = "详细实体PreTrackScale", required = true, dataType = "PreTrackScale")
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<PreTrackScale> models) {
        RESTfulResult rm = failed();
        try {
            int num = preTrackScaleService.abolish(models);
            //作废物流的运单
            for (PreTrackScale model : models) {
                try {
                    RESTfulResult result = dilFeignService.delWaybillAndPlanByPreTrackScale(FieldsCollector.beanToMap(model));
                    //system.out.println("物流作废运单接口delWaybillAndPlanByPreTrackScale完成！" + result.getResultMessage());
                } catch (Exception e) {
                    //system.out.println("物流作废运单接口delWaybillAndPlanByPreTrackScale异常！" + e.getMessage());
                }
            }
            if (num >= 1) {
                return success(num);
            } else {
                return failed("新增失败");
            }
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setFailedMessage("操作异常，请确认！" + e.getMessage());
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "非计量业务：出厂", notes = "")
    @PostMapping(value = "/noMeterOutDoor")
    public RESTfulResult noMeterOutDoor(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            String str = apppreTrackScaleService.noMeterDoor(model, "出厂");
            return success(str);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }

    @ApiOperation(value = "非计量业务：进厂", notes = "")
    @PostMapping(value = "/noMeterinDoor")
    public RESTfulResult noMeterinDoor(@RequestBody PreTrackScale model) {
        RESTfulResult rm = failed();
        try {
            String str = apppreTrackScaleService.noMeterDoor(model, "进厂");
            return success(str);
        } catch (MarkerMetException e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！");
        } finally {

        }
        return rm;
    }
}
