package com.supplier.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.supplier.model.SupplierRelease;
import com.supplier.model.SupplierReleaseDay;
import com.supplier.model.SupplierReleasePlay;
import com.supplier.model.SupplierRoute;
import com.supplier.service.ReleaseService;
import com.supplier.service.SupplierReleaseDayService;
import com.supplier.service.SupplierReleasePlayService;
import com.supplier.service.SupplierReleaseService;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 供应商路线发布
 */
@RestController
@RequestMapping("releaseCotroller")
public class ReleaseController {

    @Autowired
    private ReleaseService releaseService;
    //主表 路线基本信息
    @Autowired
    private SupplierReleaseService supplierReleaseService;
    //副表  路线每天信息
    @Autowired
    private SupplierReleaseDayService supplierReleaseDayService;
    //副表  路线游玩信息
    @Autowired
    private SupplierReleasePlayService supplierReleasePlayService;

    /**
     * 供应商发布路线
     * @param supplierRoute 参数
     * @return 返回结果
     */
    @PostMapping("addReleaseVal")
    public Map<String, Object> addReleaseVal(@RequestBody SupplierRoute supplierRoute, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplierRoute.setCode((String) userMap.get("code"));

            //生成uuid
            String id = UUID.randomUUID().toString().replace("-", "");
            supplierRoute.setUuid(id);

            //新增每天内容
            if(supplierRoute.getReleaseDayString() != null){
                List<SupplierReleaseDay> list = JSONArray.parseArray(supplierRoute.getReleaseDayString(), SupplierReleaseDay.class);
                for(SupplierReleaseDay supplierReleaseDay : list){
                    supplierReleaseDay.setUuid(id);
                    supplierReleaseService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRoute.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRoute.getReleasePlayString(), SupplierReleasePlay.class);
                for(SupplierReleasePlay supplierReleasePlay : listData){
                    supplierReleasePlay.setUuid(id);
                    supplierReleaseService.addReleasePlay(supplierReleasePlay);
                }
            }
            releaseService.addReleaseVal(supplierRoute);
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

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

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

    /**
     * 查询路线详情
     * @param supplierRoute 参数
     * @param request 参数
     * @return 返回结果
     */
    @GetMapping("getReleaseVal")
    public Map<String, Object> getReleaseVal(SupplierRoute supplierRoute, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            //获取token
            Map<String, Object> userMap = ParsingToken.tokenParsing(request);
            supplierRoute.setCode((String) userMap.get("code"));
            List<SupplierRoute> list = releaseService.getReleaseVal(supplierRoute);
            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");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    /**
     * 修改路线
     * @param supplierRoute 参数
     * @param request 参数
     * @return 返回结果
     */
    @PostMapping("updateReleaseVal")
    public Map<String, Object> updateReleaseVal(@RequestBody SupplierRoute supplierRoute, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        try{
            releaseService.updateSupplierRoute(supplierRoute);//修改

            SupplierRelease supplierRelease = new SupplierRelease().setUuid(supplierRoute.getUuid());
            //修改主表
            supplierReleaseDayService.delSupplierReleaseDay(supplierRelease);
            supplierReleasePlayService.delSupplierReleasePlay(supplierRelease);
            //新增每天内容
            if(supplierRoute.getReleaseDayString() != null){
                List<SupplierReleaseDay> list = JSONArray.parseArray(supplierRoute.getReleaseDayString(), SupplierReleaseDay.class);
                for(SupplierReleaseDay supplierReleaseDay : list){
                    supplierReleaseDay.setUuid(supplierRelease.getUuid());
                    supplierReleaseService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRoute.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRoute.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;
    }

}
