package com.supplier.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.supplier.model.*;
import com.supplier.service.PersonalServce;
import com.supplier.service.SupplierReleaseDayService;
import com.supplier.service.SupplierReleasePlayService;
import com.supplier.service.SupplierReleaseService;
import com.supplier.util.DesDecry;
import com.supplier.util.ParsingToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 供应商发布线路
 */
@RestController
@RequestMapping("supplierRelease")
public class SupplierReleaseController {
    //主表 路线基本信息
    @Autowired
    private SupplierReleaseService supplierReleaseService;
    //副表  路线每天信息
    @Autowired
    private SupplierReleaseDayService supplierReleaseDayService;
    //副表  路线游玩信息
    @Autowired
    private SupplierReleasePlayService supplierReleasePlayService;
    //供应商信息
    @Autowired
    private PersonalServce personalServce;

    /**
     * 新增路线
     * @param supplierRelease 参数
     * @param request 参数
     * @return 返回结果
     */
    @PostMapping("addSupplierRelease")
    public Map<String, Object> addRelease(@RequestBody SupplierRelease supplierRelease, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try {
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplierRelease.setCode((String) userMap.get("code"));
            //查询供应商报价单次数是否充足
            Supplier supplier = personalServce.getPresonalMessage(supplierRelease.getCode());
            if(supplier.getOfferNum() == 0){
                map.put("errMsg", "您的报价单次数不足，请先购买");
                map.put("msg", "500");
                return map;
            }
            //生成uuid
            String id = UUID.randomUUID().toString().replace("-", "");
            supplierRelease.setUuid(id);
            //新增每天内容
            if(supplierRelease.getReleaseDayString() != null){
                List<SupplierReleaseDay> list = JSONArray.parseArray(supplierRelease.getReleaseDayString(), SupplierReleaseDay.class);
                for(SupplierReleaseDay supplierReleaseDay : list){
                    supplierReleaseDay.setUuid(id);
                    supplierReleaseService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRelease.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRelease.getReleasePlayString(), SupplierReleasePlay.class);
                for(SupplierReleasePlay supplierReleasePlay : listData){
                    supplierReleasePlay.setUuid(id);
                    supplierReleaseService.addReleasePlay(supplierReleasePlay);
                }
            }
            int length = supplierRelease.getCode().length();//用户编号长度
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");//时间
            //单号
            supplierRelease.setOrderNo("PR" + supplierRelease.getCode().substring(length - 7, length) + simpleDateFormat.format(DateUtil.date()));
            supplierReleaseService.addRelease(supplierRelease);
            supplierReleaseService.addPrivateType(supplierRelease);
            //供应商报价单次数减一
            personalServce.updateOffNum(supplierRelease);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 查询供应商路线
     * @param supplierRelease 参数
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("getSupplierRelease")
    public Map<String, Object> getSupplierRelease(SupplierRelease supplierRelease, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplierRelease.setCode((String) userMap.get("code"));

            List<SupplierRelease> list = supplierReleaseService.getSupplierRelease(supplierRelease);
            for(int i = 0; i < list.size(); i++){
                //查询每日信息
                list.get(i).setReleaseDay(supplierReleaseService.getSupplierReleaseDay(list.get(i).getUuid()));
                //查询游玩游戏
                list.get(i).setReleasePlay(supplierReleaseService.getSupplierReleasePlay(list.get(i).getUuid()));
            }
            map.put("data", list);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 个人查询供应商提供的信息
     * @param supplierRelease
     * @param request
     * @return
     */
    @GetMapping("getSupplierReleasePersonal")
    public Map<String, Object> getSupplierReleasePersonal(SupplierRelease supplierRelease, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            List<SupplierRelease> list = supplierReleaseService.getSupplierRelease(supplierRelease);
            for(int i = 0; i < list.size(); i++){
                //查询每日信息
                list.get(i).setReleaseDay(supplierReleaseService.getSupplierReleaseDay(list.get(i).getUuid()));
                //查询游玩游戏
                list.get(i).setReleasePlay(supplierReleaseService.getSupplierReleasePlay(list.get(i).getUuid()));
            }
            map.put("data", list);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
        }
        return map;
    }

    /**
     * 修改供应商路线
     * @param supplierRelease 参数
     * @param request 参数
     * @return 返回结果
     */
    @PostMapping("updateSupplierRelease")
    public Map<String, Object> updateSupplierRelease(@RequestBody SupplierRelease supplierRelease, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //判断状态是否能修改 已付费后不能修改
            String orderType = supplierReleaseService.getSupplierOrderType(supplierRelease);
            if(orderType.equals("2")){
                map.put("msg", "500");
                map.put("errMsg", "该报价单已被购买，请勿修改");
                return map;
            }
            //修改主表
            supplierReleaseService.updateSupplierRelease(supplierRelease);
            supplierReleaseDayService.delSupplierReleaseDay(supplierRelease);
            supplierReleasePlayService.delSupplierReleasePlay(supplierRelease);
            //新增每天内容
            if(supplierRelease.getReleaseDayString() != null){
                List<SupplierReleaseDay> list = JSONArray.parseArray(supplierRelease.getReleaseDayString(), SupplierReleaseDay.class);
                for(SupplierReleaseDay supplierReleaseDay : list){
                    supplierReleaseDay.setUuid(supplierRelease.getUuid());
                    supplierReleaseService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRelease.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRelease.getReleasePlayString(), SupplierReleasePlay.class);
                for(SupplierReleasePlay supplierReleasePlay : listData){
                    supplierReleasePlay.setUuid(supplierRelease.getUuid());
                    supplierReleaseService.addReleasePlay(supplierReleasePlay);
                }
            }
            map.put("msg", "200");
        }catch (Exception e){
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 查询付费之后的方案单
     * @param confirmOrdering 参数
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("getConfirmOrdering")
    public Map<String, Object> getConfirmOrdering(ConfirmOrdering confirmOrdering, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            confirmOrdering.setCode((String) userMap.get("code"));
            ConfirmOrdering confirmOrdering1 = supplierReleaseService.getConfirmOrdering(confirmOrdering);
            if(null != confirmOrdering1){
                List<UsedUser> list = new ArrayList<>();
                for(UsedUser usedUser : supplierReleaseService.getConfirmUsed(confirmOrdering1.getUuid())){
                    usedUser.setIdNumber(DesDecry.decrypt(usedUser.getIdNumber()));
                    usedUser.setPhone(DesDecry.decrypt(usedUser.getPhone()));
                    list.add(usedUser);
                }
                confirmOrdering1.setUsedUserArray(list);
                confirmOrdering1.setPhone(DesDecry.decrypt(confirmOrdering1.getPhone()));
            }
            //行程
            StringBuilder trip = new StringBuilder();
            for(SupplierReleasePlay supplierReleasePlay : supplierReleaseService.getSupplierReleasePlay(confirmOrdering1.getReleaseUuid())){
                trip.append("<li data-v-7026179f=\"\">"+ supplierReleasePlay.getName() +"</li>");
            }
            confirmOrdering1.setTrip(trip.toString());
            map.put("data", confirmOrdering1);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    /**
     * 查询付费之后的方案单普通用户
     * @param confirmOrdering 参数
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("getConfirmOrderingUser")
    public Map<String, Object> getConfirmOrderingUser(ConfirmOrdering confirmOrdering, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            confirmOrdering.setCode((String) userMap.get("code"));
            ConfirmOrdering confirmOrdering1 = supplierReleaseService.getConfirmOrderingUser(confirmOrdering);
            if(null != confirmOrdering1){
                List<UsedUser> list = new ArrayList<>();
                for(UsedUser usedUser : supplierReleaseService.getConfirmUsed(confirmOrdering1.getUuid())){
                    usedUser.setIdNumber(DesDecry.decrypt(usedUser.getIdNumber()));
                    usedUser.setPhone(DesDecry.decrypt(usedUser.getPhone()));
                    list.add(usedUser);
                }
                confirmOrdering1.setUsedUserArray(list);
            }
            //行程
            StringBuilder trip = new StringBuilder();
            for(SupplierReleasePlay supplierReleasePlay : supplierReleaseService.getSupplierReleasePlay(confirmOrdering1.getReleaseUuid())){
                trip.append("<li data-v-7026179f=\"\">"+ supplierReleasePlay.getName() +"</li>");
            }
            confirmOrdering1.setTrip(trip.toString());

            map.put("data", confirmOrdering1);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    /**
     * 上传信息
     * @param supplierRelease 参数
     * @param request 参数
     * @return 返回结果
     */
    @PutMapping("updateTraffic")
    public Map<String, Object> updateTraffic(@RequestBody SupplierRelease supplierRelease, HttpServletRequest request){
        //获取token
        Map<String, Object> userMap = ParsingToken.tokenParsing(request);
        supplierRelease.setCode((String) userMap.get("code"));
        return supplierReleaseService.updateTraffic(supplierRelease);
    }
}
