package com.jeeplus.pubmodules.luckyDraw.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.pubmodules.luckyDraw.service.LuckyDrawMembersService;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawMembersDto;
import com.jeeplus.utils.StringUtils;
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.List;

/**
 * @author: 王强
 * @create: 2023-02-28 11:08
 **/
@RestController
@Api(tags ="抽奖-人员")
@RequestMapping(value = "/luckyDraw/member")
public class LuckyDrawMembersController {
    @Resource
    private LuckyDrawMembersService membersService;



    /*@ApiOperation(value = "删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id){
        String s = membersService.remove(id);
        return ResponseEntity.ok(s);
    }*/
    /**
     * 删除
     * @param ids
     * @return
     */
    @ApiLog(value = "删除")
    @DeleteMapping("remove")
    public ResponseEntity delete(String ids) {
        return membersService.deleteByIds(ids);
    }

    /**
     * 新增/修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody LuckyDrawMembersDto dto) throws Exception{
        if(StringUtils.isBlank(dto.getEventId())){
            return ResponseEntity.badRequest().body ("请选择参与的活动");
        }
        String result = membersService.save(dto);
        switch (result){
            case "1":
                return ResponseEntity.badRequest().body ("您已参加该活动。无需重复参与");
            case "2":
                return ResponseEntity.badRequest().body ("新增失败");
        }
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 查询
     * @param phone
     * @return
     */
    @ApiOperation(value = "联系电话唯一性")
    @GetMapping("/findMobilePhoneOnly")
    public ResponseEntity<Integer> findMobilePhoneOnly(@RequestParam String phone) throws Exception{
        Integer idCardOnly = membersService.findMobilePhoneOnly(phone);
        return ResponseEntity.ok(idCardOnly);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<LuckyDrawMembersDto> findById(@RequestParam String id) throws Exception{
        LuckyDrawMembersDto dto = membersService.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<LuckyDrawMembersDto>> list(Page<LuckyDrawMembersDto> page, LuckyDrawMembersDto dto) throws Exception{
        IPage<LuckyDrawMembersDto> iPage = membersService.list(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "app列表查询")
    @GetMapping("/appShowList")
    public ResponseEntity<IPage<LuckyDrawMembersDto>> appShowList(Page<LuckyDrawMembersDto> page, LuckyDrawMembersDto dto) throws Exception{
        page.setSize(-1);
        page.setCurrent(1);
        IPage<LuckyDrawMembersDto> iPage = membersService.list(page, dto);
        List<LuckyDrawMembersDto> records = iPage.getRecords();
        if(records.size()>0){
            for (int i = 0; i < records.size(); i++) {
                LuckyDrawMembersDto info = records.get(i);
                info.setSerialNumber(i +1);
            }
        }
        return ResponseEntity.ok(iPage);
    }
}
