package com.fjs.scenic.controller.scenic;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fjs.scenic.controller.common.BaseController;
import com.fjs.scenic.entity.scenic.Pics;
import com.fjs.scenic.entity.scenic.Scenic;
import com.fjs.scenic.entity.scenic.ScenicDescription;
import com.fjs.scenic.entity.scenic.TicketInformation;
import com.fjs.scenic.entity.scenic.po.ScenicPo;
import com.fjs.scenic.service.scenic.PicsService;
import com.fjs.scenic.service.scenic.ScenicDescriptionService;
import com.fjs.scenic.service.scenic.ScenicService;
import com.fjs.scenic.service.scenic.TicketInformationService;
import com.fjs.scenic.utils.ReturnResult;

/**
 * This class is used for ...
 * 景区
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@RestController
@RequestMapping("/scenic")
public class ScenicController extends BaseController<Scenic> {
    @Autowired
    private PicsService picsService;
    @Autowired
    private ScenicService scenicService;
    @Autowired
    private ScenicDescriptionService scenicDescriptionService;
    @Autowired
    private TicketInformationService ticketInformationService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/index")
    public ModelAndView index(HttpSession session) {
        return new ModelAndView("/scenic/scenic");
    }

    @GetMapping("/get/{id}")
    @Override
    public ReturnResult get(@PathVariable("id") Integer id) {
        return  ReturnResult.ok(scenicService.find(id));
    }

    @PostMapping("/edit")
    @ResponseBody
    public ReturnResult edit(@RequestBody ScenicPo scenicPo) {
        Scenic scenic = new Scenic(scenicPo.getId(), scenicPo.getName(), scenicPo.getGrade(), scenicPo.getIntro(),
                scenicPo.getAddr(), scenicPo.getLon(), scenicPo.getLat(), scenicPo.getPhone(),
                scenicPo.getVoidUrl(),scenicPo.getVrUrl(),scenicPo.getBusinessTime());
        ScenicDescription scenicDescription = new ScenicDescription(scenicPo.getId(), scenicPo.getDescription());
        TicketInformation ticketInformation = new TicketInformation(scenicPo.getId(),scenicPo.getContent());
        List<Pics> picsList = new ArrayList<>();
        for (Pics p : scenicPo.getPics()){
            p.setId(null);
            picsList.add(p);
        }

        picsService.update(picsList);
        scenicService.update(scenic);
        scenicDescriptionService.update(scenicDescription);
        ticketInformationService.updateByScenicId(ticketInformation);
        Set<String> keys = redisTemplate.keys("indexTopData:" + "*");
        redisTemplate.delete(keys);
        return  ReturnResult.ok();
    }

    @PostMapping("/editScenic")
    public ReturnResult editScenic(Scenic scenic) {
    	 Set<String> keys = redisTemplate.keys("indexTopData:" + "*");
         redisTemplate.delete(keys);
        return  ReturnResult.ok(scenicService.update(scenic));
    }

    @PostMapping("/addPic")
    public ReturnResult addPic(Pics record) {
        return  ReturnResult.ok(picsService.add(record));
    }

    @GetMapping("/getPic/{fid}")
    public ReturnResult getPic(@PathVariable("fid") Integer fid) {
        return  ReturnResult.ok(picsService.getPic(fid));
    }

    @GetMapping("/delPic/{id}")
    public ReturnResult delPic(@PathVariable("id") Integer id) {
        return  ReturnResult.ok(picsService.del(id));
    }
    
}
