package com.jeeplus.pubmodules.luckyDraw.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.pubmodules.luckyDraw.service.LuckyDrawInfoService;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawInfoDto;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawMembersDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王强
 * @create: 2023-02-28 11:06
 **/
@RestController
@Api(tags ="抽奖-抽奖")
@RequestMapping(value = "/luckyDraw/info")
public class LuckyDrawInfoController {

    @Resource
    private LuckyDrawInfoService drawInfoService;

    /**
     * 根据活动id查询抽奖信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据活动id查询抽奖信息")
    @GetMapping("/findByEventId")
    public ResponseEntity<LuckyDrawInfoDto> findByEventId(@RequestParam String id) throws Exception{
        LuckyDrawInfoDto dto = drawInfoService.findByEventId(id);
        return ResponseEntity.ok(dto);
    }

    @ApiLog("活动列表")
    @GetMapping("list")
    public ResponseEntity<IPage<LuckyDrawInfoDto>> data(LuckyDrawInfoDto luckyDrawEventsDTO, Page<LuckyDrawInfoDto> page) throws Exception {
        IPage<LuckyDrawInfoDto> result = new Page<LuckyDrawInfoDto>();
        result = drawInfoService.findList (page,luckyDrawEventsDTO);
        return ResponseEntity.ok (result);
    }

    @ApiLog("数据拼接")
    @GetMapping("pjList")
    public ResponseEntity<IPage<LuckyDrawInfoDto>> pjList(LuckyDrawInfoDto luckyDrawEventsDTO, Page<LuckyDrawInfoDto> page) throws Exception {
        page.setSize(-1);
        page.setCurrent(1);
        IPage<LuckyDrawInfoDto> result = new Page<LuckyDrawInfoDto>();
        result = drawInfoService.findList (page,luckyDrawEventsDTO);
        List<LuckyDrawInfoDto> records = result.getRecords();
        List<LuckyDrawInfoDto> addList = new ArrayList<>();
        //获取已抽奖人数和本活动剩余未抽奖人数
       //drawInfoService.getLuckyDrawCount(luckyDrawEventsDTO);
        records.forEach(re ->{
            if(re.getLuckyDrawMembersDtos().size()>0){
                re.setLuckyDrawFlag("1");
                re.setShowFlag("2");
                re.setLuckyDrawAgainFlag("1");
            }else{
                re.setLuckyDrawFlag("0");
                re.setShowFlag("1");
                re.setLuckyDrawAgainFlag("0");
            }
            addList.add(re);

            LuckyDrawInfoDto btn = new LuckyDrawInfoDto(); // 新增按钮行
            btn.setIsButton("1"); // 标记此行数据是按钮
            if(re.getLuckyDrawMembersDtos().size()>0){
                btn.setLuckyDrawFlag("1");
                btn.setLuckyDrawAgainFlag("1");
            }else{
                btn.setLuckyDrawFlag("0");
                btn.setLuckyDrawAgainFlag("0");
            }
            btn.setEventsId(re.getEventsId());
            btn.setNumber(re.getNumber());
            btn.setId(re.getId());
            addList.add(btn);

            if(re.getLuckyDrawMembersDtos().size()>0){
                LuckyDrawInfoDto sh = new LuckyDrawInfoDto();
                sh.setAwardsName("姓名");
                sh.setNumber("手机号");
                sh.setPrizeName("部门");
                sh.setShowFlag("1");
                sh.setLuckyDrawFlag("1");
                addList.add(sh);
            }else{
                re.setLuckyDrawFlag("0");
                re.setShowFlag("2");
            }

            if(re.getLuckyDrawMembersDtos().size()>0){
                for (int i = 0; i< re.getLuckyDrawMembersDtos().size(); i++){
                    LuckyDrawMembersDto mem = re.getLuckyDrawMembersDtos().get(i);
                    LuckyDrawInfoDto l = new LuckyDrawInfoDto();
                    l.setAwardsName(mem.getName());
                    l.setNumber(mem.getPhone());
                    l.setPrizeName(mem.getOfficeName());
                    l.setShowFlag("0");
                    l.setLuckyDrawFlag("1");
                    l.setSerialNumber(i + 1);
                    addList.add(l);
                }
            }
        });
        result.setRecords(addList);
        return ResponseEntity.ok (result);
    }

    @ApiLog("数据拼接(中奖展示)")
    @GetMapping("pjShowList")
    public ResponseEntity<IPage<LuckyDrawInfoDto>> pjShowList(LuckyDrawInfoDto luckyDrawEventsDTO, Page<LuckyDrawInfoDto> page) throws Exception {
        page.setSize(-1);
        page.setCurrent(1);
        IPage<LuckyDrawInfoDto> result = new Page<LuckyDrawInfoDto>();
        result = drawInfoService.findList (page,luckyDrawEventsDTO);
        List<LuckyDrawInfoDto> records = result.getRecords();
        List<LuckyDrawInfoDto> addList = new ArrayList<>();
        //获取已抽奖人数和本活动剩余未抽奖人数
       //drawInfoService.getLuckyDrawCount(luckyDrawEventsDTO);
        records.forEach(re ->{
            if(re.getLuckyDrawMembersDtos().size()>0){
                re.setLuckyDrawFlag("1");
                re.setShowFlag("2");
                re.setLuckyDrawAgainFlag("1");
            }else{
                re.setLuckyDrawFlag("0");
                re.setShowFlag("1");
                re.setLuckyDrawAgainFlag("0");
            }
            addList.add(re);
            if(re.getLuckyDrawMembersDtos().size()>0){
                LuckyDrawInfoDto sh = new LuckyDrawInfoDto();
                sh.setAwardsName("姓名");
                sh.setNumber("手机号");
                sh.setPrizeName("部门");
                sh.setShowFlag("1");
                sh.setLuckyDrawFlag("1");
                addList.add(sh);
            }else{
                re.setLuckyDrawFlag("0");
                re.setShowFlag("2");
            }

            if(re.getLuckyDrawMembersDtos().size()>0){
                for (int i = 0; i< re.getLuckyDrawMembersDtos().size(); i++){
                    LuckyDrawMembersDto mem = re.getLuckyDrawMembersDtos().get(i);
                    LuckyDrawInfoDto l = new LuckyDrawInfoDto();
                    l.setAwardsName(mem.getName());
                    l.setNumber(mem.getPhone());
                    l.setPrizeName(mem.getOfficeName());
                    l.setShowFlag("0");
                    l.setLuckyDrawFlag("1");
                    l.setSerialNumber(i + 1);
                    addList.add(l);
                }
            }

            /*re.getLuckyDrawMembersDtos().forEach(mem->{
                LuckyDrawInfoDto l = new LuckyDrawInfoDto();
                l.setAwardsName(mem.getName());
                l.setNumber(mem.getPhone());
                l.setPrizeName(mem.getOfficeName());
                l.setShowFlag("0");
                l.setLuckyDrawFlag("1");
                addList.add(l);
            });*/
        });
        result.setRecords(addList);
        return ResponseEntity.ok (result);
    }

    /**
     * 抽奖
     * @param dto
     * @return
     */
    @ApiOperation(value = "抽奖")
    @PostMapping("/luckyDraw")
    public ResponseEntity<String> luckyDraw(@RequestBody LuckyDrawInfoDto dto) throws Exception{
        String s = drawInfoService.luckyDraw(dto);
        return ResponseUtil.newInstance().ok (s);
    }

    /**
     * 重新抽奖
     * @param dto
     * @return
     */
    @ApiOperation(value = "抽奖")
    @PostMapping("/luckyDrawAgain")
    public ResponseEntity<String> luckyDrawAgain(@RequestBody LuckyDrawInfoDto dto) throws Exception{
        String s = drawInfoService.luckyDrawAgain(dto);
        return ResponseUtil.newInstance().ok (s);
    }

    /**
     * 重新抽奖
     * @param dto
     * @return
     */
    @ApiOperation(value = "清除抽奖结果信息")
    @PostMapping("/luckyDrawClear")
    public ResponseEntity<String> luckyDrawClear(@RequestBody LuckyDrawInfoDto dto) throws Exception{
        String s = drawInfoService.luckyDrawClear(dto);
        return ResponseUtil.newInstance().ok (s);
    }

}
