package com.ruoyi.web.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.exception.ErrorMessageException;
import com.ruoyi.web.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动信息Controller
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/hd/info")
@Validated
public class HdInfoController extends BaseController
{
    private IHdInfoService hdInfoService;
    @Autowired
    public void setHdInfoService(IHdInfoService hdInfoService) {
        this.hdInfoService = hdInfoService;
    }

    private IHdQdService hdQdService;
    @Autowired
    public void setHdQdService(IHdQdService hdQdService) {
        this.hdQdService = hdQdService;
    }

    private IHdZdService hdZdService;
    @Autowired
    public void setHdZdService(IHdZdService hdZdService) {
        this.hdZdService = hdZdService;
    }

    private IHdQhService hdQhService;
    @Autowired
    public void setHdQhService(IHdQhService hdQhService) {
        this.hdQhService = hdQhService;
    }

    private HdInfo getHdInfo(Date date){
        QueryWrapper<HdInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(HdInfo::getHdrq,date);
        return hdInfoService.getOne(qw);
    }

    @GetMapping("/user/wqd/{id}")
    @ApiOperation("未签到用户列表")
    public AjaxResult hdWqd(@PathVariable Long id){
        return success(hdInfoService.hdWqd(id));
    }

    @PostMapping("/user/bq")
    @ApiOperation("帮签")
    public AjaxResult bq(@RequestBody HdQd hdQd){
        List<HdQd> hdQdList = new ArrayList<>();
        hdQd.getUserIdList().forEach(userId->{
            HdQd newHdQd = new HdQd();
            newHdQd.setUserId(userId);
            newHdQd.setFid(hdQd.getFid());
            newHdQd.setQdsj(DateUtils.getNowDate());
            hdQdList.add(newHdQd);
        });
        hdQdService.saveBatch(hdQdList);
        return success();
    }

    @PostMapping("/user/bqq")
    @ApiOperation("帮取签")
    public AjaxResult bqq(@RequestBody HdQd hdQd){
        QueryWrapper<HdQd> qw = new QueryWrapper<>();
        qw.lambda().eq(HdQd::getFid,hdQd.getFid()).eq(HdQd::getUserId,hdQd.getUserId());
        hdQdService.remove(qw);
        return success();
    }

    @GetMapping("/user/{id}")
    @ApiOperation("活动参与人员")
    public AjaxResult hdUser(@PathVariable Long id){
        return success(hdInfoService.hdUser(id));
    }

    @GetMapping("/user/grls")
    @ApiOperation("个人历史")
    public AjaxResult grls(){
        QueryWrapper<HdQd> hqQW = new QueryWrapper<>();
        hqQW.lambda().eq(HdQd::getUserId,SecurityUtils.getUserId());
        Set<Long> hdIdSet = hdQdService.list(hqQW).stream().map(HdQd::getFid).collect(Collectors.toSet());
        if (hdIdSet.isEmpty()) return success(Collections.emptyList());
        QueryWrapper<HdInfo> qw = new QueryWrapper<>();
        qw.lambda().in(HdInfo::getId,hdIdSet).orderByDesc(HdInfo::getHdrq);
        return success(hdInfoService.list(qw));
    }

    @GetMapping("/user/verify")
    @ApiOperation("校验是否签到")
    public AjaxResult verifyQd(String date) {
        Date hdrq = DateUtils.getNowDate();
        if(StringUtils.isNotEmpty(date)) hdrq = DateUtils.parseDate(date);
        HdInfo hdInfo = getHdInfo(hdrq);
        if (Objects.isNull(hdInfo)) return success(false);
        QueryWrapper<HdQd> qw = new QueryWrapper<>();
        qw.lambda().eq(HdQd::getUserId,SecurityUtils.getUserId()).eq(HdQd::getFid,hdInfo.getId());
        HdQd hdQd = hdQdService.getOne(qw);
        if (Objects.isNull(hdQd)) return success(false);
        return success(true);
    }

    @PostMapping("/user/qd")
    @ApiOperation("签到")
    public AjaxResult userQd(String date){
        Date hdrq = DateUtils.getNowDate();
        if(StringUtils.isNotEmpty(date)) hdrq = DateUtils.parseDate(date);
        HdInfo hdInfo = getHdInfo(hdrq);
        if (Objects.isNull(hdInfo)) {
            hdInfo = new HdInfo();
            hdInfo.setHdrq(hdrq);
            hdInfoService.save(hdInfo);
        }
        if (getJszt(hdInfo.getId())) throw new ErrorMessageException("账单已结算，禁止签到");
        HdQd hdQd = new HdQd();
        hdQd.setFid(hdInfo.getId());
        hdQd.setUserId(SecurityUtils.getUserId());
        hdQd.setQdsj(DateUtils.getNowDate());
        hdQdService.save(hdQd);
        return success();
    }

    @PostMapping("/user/qd/cancel")
    @ApiOperation("取消签到")
    public AjaxResult userQdCancel(String date){
        Date hdrq = DateUtils.getNowDate();
        if(StringUtils.isNotEmpty(date)) hdrq = DateUtils.parseDate(date);
        HdInfo hdInfo = getHdInfo(hdrq);
        if (getJszt(hdInfo.getId())) throw new ErrorMessageException("账单已结算，禁止取消签到");
        QueryWrapper<HdQd> qw = new QueryWrapper<>();
        qw.lambda().eq(HdQd::getUserId,SecurityUtils.getUserId()).eq(HdQd::getFid,hdInfo.getId());
        hdQdService.remove(qw);
        return success();
    }

    /**
     * 查询活动信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HdInfo hdInfo)
    {
        startPage();
        QueryWrapper<HdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(hdInfo);
        List<HdInfo> hdInfoList = hdInfoService.list(queryWrapper);
        return getDataTable(hdInfoList);
    }

    /**
     * 导出活动信息列表
     */
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdInfo hdInfo)
    {
        QueryWrapper<HdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(hdInfo);
        List<HdInfo> list = hdInfoService.list(queryWrapper);
        ExcelUtil<HdInfo> util = new ExcelUtil<>(HdInfo.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        HdInfo hdInfo = hdInfoService.getById(id);
        hdInfo.setJszt(getJszt(hdInfo.getId()));
        QueryWrapper<HdQh> hqQW = new QueryWrapper<>();
        hqQW.lambda().eq(HdQh::getHdId,id);
        hdInfo.setHdQhList(hdQhService.list(hqQW));
        return success(hdInfo);
    }

    private boolean getJszt(Long id){
        QueryWrapper<HdZd> qw = new QueryWrapper<>();
        qw.lambda().eq(HdZd::getHdId,id);
        HdZd hdZd = hdZdService.getOne(qw);
        return !Objects.isNull(hdZd);
    }

    /**
     * 新增活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdInfo hdInfo)
    {
        hdInfoService.save(hdInfo);
        return success(hdInfo);
    }

    /**
     * 修改活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HdInfo hdInfo)
    {
        hdInfoService.updateById(hdInfo);
        return success(hdInfo);
    }

    /**
     * 删除活动信息
     */
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hdInfoService.removeByIds(Arrays.asList(ids)));
    }
}
