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.TemplateService;
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("template")
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    /**
     * 新增路线
     * @param supplierRelease 参数
     * @param request 参数
     * @return 返回结果
     */
    @PostMapping("addSupplierRelease")
    public Map<String, Object> addRelease(@RequestBody SupplierRelease supplierRelease, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        System.out.println(supplierRelease.getReleasePlayString());
        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);
                    templateService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRelease.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRelease.getReleasePlayString(), SupplierReleasePlay.class);
                for(SupplierReleasePlay supplierReleasePlay : listData){
                    supplierReleasePlay.setUuid(id);
                    templateService.addReleasePlay(supplierReleasePlay);
                }
            }
            templateService.addRelease(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 = templateService.getSupplierRelease(supplierRelease);
            for(int i = 0; i < list.size(); i++){
                //查询每日信息
                list.get(i).setReleaseDay(templateService.getSupplierReleaseDay(list.get(i).getUuid()));
                //查询游玩游戏
                list.get(i).setReleasePlay(templateService.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{
            //修改主表
            templateService.updateSupplierRelease(supplierRelease);
            templateService.delSupplierReleaseDay(supplierRelease);
            templateService.delSupplierReleasePlay(supplierRelease);
            //新增每天内容
            if(supplierRelease.getReleaseDayString() != null){
                List<SupplierReleaseDay> list = JSONArray.parseArray(supplierRelease.getReleaseDayString(), SupplierReleaseDay.class);
                for(SupplierReleaseDay supplierReleaseDay : list){
                    supplierReleaseDay.setUuid(supplierRelease.getUuid());
                    templateService.addReleaseDay(supplierReleaseDay);
                }
            }
            //新增游玩内容
            if(supplierRelease.getReleasePlayString() != null){
                List<SupplierReleasePlay> listData = JSONArray.parseArray(supplierRelease.getReleasePlayString(), SupplierReleasePlay.class);
                for(SupplierReleasePlay supplierReleasePlay : listData){
                    supplierReleasePlay.setUuid(supplierRelease.getUuid());
                    templateService.addReleasePlay(supplierReleasePlay);
                }
            }
            map.put("msg", "200");
        }catch (Exception e){
            map.put("msg", "500");
            e.printStackTrace();
        }
        return map;
    }

}
