package com.lz.modules.luck.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.luck.entity.*;
import com.lz.modules.luck.service.LuckGoodsService;
import com.lz.modules.luck.service.LuckRecordService;
import com.lz.modules.luck.service.LuckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/luck")
public class LuckController {


    @Autowired
    private LuckService luckService;

    @Autowired
    private LuckGoodsService luckGoodsService;

    @Autowired
    private LuckRecordService luckRecordService;


    @RequestMapping("/list")
     public R list(@RequestBody String body) {

        return R.ok();
     }


     @RequestMapping("/getById")
     public R getById(@RequestBody Luck luck) {
        luck = luckService.selectLuckById(luck.getId());
        return R.ok().put("luck",luck);
     }

    @GetMapping("/getLuckById")
    public R getLuckById(@RequestParam Long luckId) {
        return getLuck(luckId);
    }

    private R getLuck(Long luckId){
        Luck luck = luckService.selectLuckById(luckId);
        if(luck != null){
            LuckDetailInfoReq luckDetailInfoReq = new LuckDetailInfoReq();
            if(luck.getFinish().intValue() == 0){//活动未结束
                luckDetailInfoReq.setId(luckId);
                luckDetailInfoReq.setName(luck.getName());
                List<LuckGoods> luckGoods = luckGoodsService.getNoFinishByLuckId(luckId);
                if(luckGoods.size() > 0){
                    List<LuckGoodsReq>  luckGoodsReqs = new ArrayList<>();
                    for (LuckGoods luckGood:luckGoods
                    ) {
                        LuckGoodsReq luckGoodsReq = new LuckGoodsReq();
                        luckGoodsReq.setCounts(luckGood.getCounts());
                        luckGoodsReq.setId(luckGood.getId());
                        luckGoodsReq.setName(luckGood.getName());
                        luckGoodsReq.setWeight(luckGood.getWeight());
                        luckGoodsReqs.add(luckGoodsReq);
                    }
                    luckDetailInfoReq.setLuckGoodsReqs(luckGoodsReqs);
                    List<LuckRecord> luckRecords = luckRecordService.selectNoGoodLuckRecordByLuckId(String.valueOf(luckId));
                    if(luckRecords.size() > 0){
                        List<LuckRecordReq> luckRecordReqList = new ArrayList<>();
                        for (LuckRecord luckRecord:luckRecords
                        ) {
                            LuckRecordReq luckRecordReq = new LuckRecordReq();
                            luckRecordReq.setName(luckRecord.getName());
                            luckRecordReq.setId(luckRecord.getId());
                            luckRecordReq.setGoodsId(luckRecord.getGoodsId());
                            luckRecordReq.setGoodsName(luckRecord.getGoodsName());
                            luckRecordReq.setIsLuck(luckRecord.getIsLuck());
                            luckRecordReq.setWeight(luckRecord.getWeight());
                            luckRecordReq.setMobile(luckRecord.getMobile());
                            luckRecordReqList.add(luckRecordReq);
                        }
                        luckDetailInfoReq.setLuckRecordReqs(luckRecordReqList);
                        return R.ok().put("luck",luckDetailInfoReq);
                    }
                    return R.error("活动暂无人员报名");
                }
                return R.error("活动无相关奖品信息");
            }else{
                //获取获奖人员信息
                luckDetailInfoReq.setIsFinish(1);
                luckDetailInfoReq.setId(luckId);
                luckDetailInfoReq.setName(luck.getName());
                List<LuckRecord> luckRecords = luckRecordService.selectGoodLuckRecordByLuckId(String.valueOf(luckId));
                if(luckRecords.size() > 0) {
                    List<LuckRecordReq> luckRecordReqList = new ArrayList<>();
                    for (LuckRecord luckRecord : luckRecords
                    ) {
                        LuckRecordReq luckRecordReq = new LuckRecordReq();
                        luckRecordReq.setName(luckRecord.getName());
                        luckRecordReq.setId(luckRecord.getId());
                        luckRecordReq.setGoodsId(luckRecord.getGoodsId());
                        luckRecordReq.setGoodsName(luckRecord.getGoodsName());
                        luckRecordReq.setIsLuck(luckRecord.getIsLuck());
                        luckRecordReq.setWeight(luckRecord.getWeight());
                        luckRecordReq.setMobile(luckRecord.getMobile());
                        luckRecordReqList.add(luckRecordReq);
                    }
                    luckDetailInfoReq.setLuckRecordReqs(luckRecordReqList);
                    return R.ok().put("luck", luckDetailInfoReq);
                }
            }
        }
        return R.error("无相关活动");
    }

    @PostMapping("/updateLuck")
    public R updateLuck(@RequestBody String body) {
        JSONArray jsonArray = JSONObject.parseArray(body);
        Long luckId = 0L;
        for(int i = 0; i < jsonArray.size(); i++){
            JSONObject json = jsonArray.getJSONObject(i);
            LuckRecordReq luckRecordReq = json.toJavaObject(LuckRecordReq.class);
            luckId = luckRecordReq.getLuckId();
            luckRecordService.updateLuckRecordByReqWithId(luckRecordReq);
        }
        int count0 = luckRecordService.selectCountRecordByLuckId(luckId);

        int count = luckRecordService.selectCountGoodLuckRecordByLuckId(luckId);

        int count1 = luckGoodsService.selectNumGoodLuckRecordByLuckId(luckId);
        if(count < count1 && count != count0){
            //获取未中奖人列表

            return getLuck(luckId);
        }else{
            //设置中将结束
            luckService.setLuckFinishById(luckId);
            Luck luck = luckService.selectLuckById(luckId);
            LuckDetailInfoReq luckDetailInfoReq = new LuckDetailInfoReq();
            luckDetailInfoReq.setIsFinish(1);
            luckDetailInfoReq.setName(luck.getName());
            luckDetailInfoReq.setId(luck.getId());
            return R.ok().put("luck", luckDetailInfoReq);
        }
    }



    @RequestMapping("/update")
     public R update(@RequestBody Luck luck) {
         luckService.updateLuckById(luck);
        return R.ok();
     }


     @RequestMapping("/save")
        public R save(@RequestBody Luck luck) {
         luckService.insertLuck(luck);
        return R.ok();
     }


    @RequestMapping("/delete")
    public R list(@RequestBody Long id) {
        luckService.deleteLuckById(id);
        return R.ok();
     }
}
