package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.app.dto.*;
import com.fs.app.service.AppServiceTicketService;
import com.fs.app.vo.ServiceTaskVo;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.redis.RedisCache;
import com.fs.provider.enums.BalanceBusinessType;
import com.fs.common.exception.CustomException;
import com.fs.doctor.domain.Doctor;
import com.fs.doctor.domain.DoctorBalanceLog;
import com.fs.doctor.service.IDoctorBalanceLogService;
import com.fs.doctor.service.IDoctorService;
import com.fs.provider.domain.ServiceOrder;
import com.fs.provider.enums.BalanceChangeTypeEnum;
import com.fs.provider.service.IServiceOrderService;
import com.fs.provider.service.IServiceOrderTaskRelService;
import com.fs.provider.vo.ServiceOrderInfoVO;
import com.fs.task.service.ITaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;


@Api("app服务单管理接口")
@RestController
@RequestMapping(value = "/app/serviceInfo")
public class AppServiceTicketController extends AppBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IServiceOrderService serviceOrderService;
    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private AppServiceTicketService appServiceTicketService;





    @ApiOperation("查询服务单列表")
    @PostMapping("/getServiceTicketList")
    @Login
    public AjaxResult getServiceTicketList(ServiceTicketDto serviceTicketDto) {
        String userId = getUserId();
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setDoctorId(userId);
        serviceOrder.setDoctorConfirm(serviceTicketDto.getDoctorConfirm());
        List<ServiceOrder> serviceOrders = serviceOrderService.selectServiceOrderList(serviceOrder);
        Map<YearMonth, List<ServiceOrder>> groupedByMonth = serviceOrders.stream()
                .collect(Collectors.groupingBy(
                        order -> {
                            LocalDateTime ldt = order.getAuditTime().toInstant()
                                    .atZone(ZoneId.systemDefault())
                                    .toLocalDateTime();
                            return YearMonth.from(ldt);
                        },
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> {
                                    list.sort(Comparator.comparing(ServiceOrder::getAuditTime));
                                    return list;
                                }
                        )
                ));

        return AjaxResult.success(groupedByMonth);
    }


    @Login
    @ApiOperation("手动确认服务单接口")
    @PostMapping("/confirmServiceTicket")
    public AjaxResult confirmServiceTicket(@RequestBody ConfirmTicketDto confirmTicketDto) {
        confirmTicketDto.setUserId(getUserId());
        appServiceTicketService.confirmServiceTicket(confirmTicketDto);

        return AjaxResult.success("服务确认成功");
    }

    @Login
    @ApiOperation("根据服务单id查询详情和任务列表")
    @PostMapping("/getServiceTask")
    public AjaxResult getServiceTask(@RequestBody ServiceTicketDto serviceTicketDto) {
        String userId = getUserId();
        String desc1 = "尊敬的用户:本次劳务内容:";
        String desc2 = "甲乙双方已签订《服务协议》，乙方实际向甲方提供上述服务，现双方确认该工作已全部完成。乙方确认该笔积分收入为自己的合理劳动所得，并同意依法纳税，乙方提供银行卡、身份证信息给甲方用于报税，甲方承诺为乙方个人信息保密。";
        String desc3 = "我司将向您支付积分[1534]个，作为此次劳务报酬，款项将汇入如下账户:";
        String desc4 = "承诺上述服务已真实发生;";
        String desc5 = "[适用于提供讲课服务的情形】在提供本次服务之前,已充分悉知和了解讲课内容的材料。";

        Doctor doctor = doctorService.selectDoctorById(Long.parseLong(userId));
        ServiceOrderInfoVO serviceOrderInfoVO = serviceOrderService.selectServiceOrderById(serviceTicketDto.getServiceOrderId());
        if (serviceOrderInfoVO == null) {
            throw new CustomException("服务单不存在");
        }

        ServiceTaskVo serviceTaskVo = new ServiceTaskVo();
        serviceTaskVo.setBankName(doctor.getBankName());
        serviceTaskVo.setBankBranch(doctor.getBankBranch());
        serviceTaskVo.setServiceOrderInfoVO(serviceOrderInfoVO);


        return AjaxResult.success(serviceTaskVo);
    }


}
