package com.ruoyi.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.web.domain.ZdUser;
import com.ruoyi.web.domain.dto.GrzdDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.web.domain.ZdInfo;
import com.ruoyi.web.service.IZdInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账单信息Controller
 * 
 * @author shenzx
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/zd/info")
public class ZdInfoController extends BaseController
{
    private IZdInfoService zdInfoService;
    @Autowired
    public void setZdInfoService(IZdInfoService zdInfoService) {
        this.zdInfoService = zdInfoService;
    }

    @GetMapping("grzd")
    @ApiOperation("个人账单")
    public AjaxResult grzd(){
        return success(zdInfoService.grzd());
    }

    @GetMapping("/grzd/{id}")
    @ApiOperation("个人账单详情")
    public AjaxResult grzdDetail(@PathVariable Long id){
        Optional<GrzdDTO> optional = zdInfoService.grzd().stream().filter(v -> id.equals(v.getId())).findFirst();
        if (optional.isPresent()) return success(optional.get());
        return success();
    }

    @GetMapping("/grzd/hd/{id}")
    @ApiOperation("个人账单活动列表")
    public AjaxResult grzdHd(@PathVariable Long id){
        return success(zdInfoService.grzdHd(id));
    }

    @PutMapping("/zfzt/{id}")
    @ApiOperation("更新支付状态")
    public AjaxResult updateZfzt(@PathVariable Long id){
        zdInfoService.updateZfzt(id);
        return success();
    }

    @PutMapping("/deal")
    @ApiOperation("更新交易信息标识")
    public AjaxResult updateDealId(@RequestBody ZdUser zdUser){
        zdInfoService.updateDealId(zdUser);
        return success();
    }

    @PostMapping("gen")
    @ApiOperation("生成账单")
    @Scheduled(cron = "0 0 0 15 * ?")
    public void gen(){
        zdInfoService.gen();
    }

    /**
     * 查询账单信息列表
     */
    @PreAuthorize("@ss.hasPermi('zd:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZdInfo zdInfo)
    {
        startPage();
        QueryWrapper<ZdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(zdInfo);
        List<ZdInfo> zdInfoList = zdInfoService.list(queryWrapper);
        return getDataTable(zdInfoList);
    }

    /**
     * 导出账单信息列表
     */
    @PreAuthorize("@ss.hasPermi('zd:info:export')")
    @Log(title = "账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZdInfo zdInfo)
    {
        QueryWrapper<ZdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(zdInfo);
        List<ZdInfo> list = zdInfoService.list(queryWrapper);
        ExcelUtil<ZdInfo> util = new ExcelUtil<>(ZdInfo.class);
        util.exportExcel(response, list, "账单信息数据");
    }

    /**
     * 获取账单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('zd:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(zdInfoService.getById(id));
    }

    /**
     * 新增账单信息
     */
    @PreAuthorize("@ss.hasPermi('zd:info:add')")
    @Log(title = "账单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZdInfo zdInfo)
    {
        zdInfoService.save(zdInfo);
        return success(zdInfo);
    }

    /**
     * 修改账单信息
     */
    @PreAuthorize("@ss.hasPermi('zd:info:edit')")
    @Log(title = "账单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZdInfo zdInfo)
    {
        zdInfoService.updateById(zdInfo);
        return success(zdInfo);
    }

    /**
     * 删除账单信息
     */
    @PreAuthorize("@ss.hasPermi('zd:info:remove')")
    @Log(title = "账单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zdInfoService.removeByIds(Arrays.asList(ids)));
    }
}
