package com.staffing.web.controller.merchant;

import com.staffing.common.core.controller.BaseController;
import com.staffing.common.core.domain.AjaxResult;
import com.staffing.common.core.page.TableDataInfo;
import com.staffing.custom.domain.StEmployeeFundRecord;
import com.staffing.custom.domain.StTask;
import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.param.FxParam;
import com.staffing.custom.service.IStEmployeeFundRecordService;
import com.staffing.custom.service.IStTaskService;
import com.staffing.custom.service.IStTaskSignService;
import com.staffing.custom.service.TransferBatchService;
import com.wechat.pay.java.core.notification.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * 任务Controller
 * 
 * @author staffing
 * @date 2024-08-27
 */
@RestController
@RequestMapping("/merchant/fund")
public class MerchantFundController extends BaseController
{
    @Autowired
    private IStTaskService stTaskService;
    @Autowired
    private IStTaskSignService stTaskSignService;

    @Autowired
    private IStEmployeeFundRecordService stEmployeeFundRecordService;

    @Autowired
    private TransferBatchService transferBatchService;

//    @GetMapping("/list")
//    @PreAuthorize("@ss.hasPermi('task:edit')")
//    public TableDataInfo list(StTask stTask)
//    {
//        startPage();
//        stTask.setStatus(1);
//        // 获取当前时间
//        ZonedDateTime now = ZonedDateTime.now();
//        // 将当前时间加上一天，‌然后设置为0点0分
//        ZonedDateTime nextDayStart = now.plusDays(1).truncatedTo(ChronoUnit.DAYS);
//        Date nextDayZero = Date.from(nextDayStart.toInstant());
//        if (stTask.getDateStatus() != 0){
//            stTask.setTaskDateCompare(nextDayZero);
//        }
//        stTask.getParams().put("order", nextDayZero);
//        List<StTask> list = stTaskService.selectStTaskList(stTask, 0);
//        return getDataTable(list);
//    }

    /**
     * 获取任务详细信息
     */
    @GetMapping(value = "/details/{id}")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stTaskService.selectStTaskById(id));
    }

    /**
     * 修改签到
     */
    @PutMapping("/editSign")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult edit(@RequestBody StTaskSign stTaskSign)
    {
        return toAjax(stTaskSignService.updateStTaskSign(stTaskSign));
    }

    @PutMapping("/fx")
    @PreAuthorize("@ss.hasPermi('task:edit')")
    public AjaxResult fx(@RequestBody FxParam fxParam)
    {
        return success(stEmployeeFundRecordService.faxin(fxParam));
    }

    @PostMapping("/fxResult")
    public ResponseEntity.BodyBuilder fxResult(HttpServletRequest request, @RequestBody String body)
    {

        StringBuilder headers = new StringBuilder();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            headers.append(headerName).append(": ").append(headerValue).append("\n");
        }

        logger.info("fxResult=====Headers:\n" + headers.toString() + "\nBody:\n" + body);

        RequestParam requestParam = new RequestParam.Builder()
                .serialNumber("123")
                .nonce("aaaa")
                .signature("aaaa")
                .timestamp("1234")
                .body(body)
                .build();
        return transferBatchService.parse(requestParam);
    }
}
