package com.supplier.controller;

import com.alibaba.fastjson.JSONArray;
import com.supplier.model.SupplierRelease;
import com.supplier.model.SupplierReleaseDay;
import com.supplier.model.SupplierReleasePlay;
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.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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

    /**
     * 新增路线
     * @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"));

            //生成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);
                }
            }
            supplierReleaseService.addRelease(supplierRelease);
            supplierReleaseService.addPrivateType(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{
            //修改主表
            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");
            e.printStackTrace();
        }
        return map;
    }
}
