package com.younike.member;

import com.younike.common.constant.Constants;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.exception.ServiceException;
import com.younike.framework.config.AnonymousAccess;
import com.younike.framework.config.MemberAccess;
import com.younike.framework.web.domain.MemberUserContext;
import com.younike.framework.web.service.JwtService;
import com.younike.hlpay.service.IPayService;
import com.younike.system.domain.ServerTimeBook;
import com.younike.system.service.IServerTimeBookService;
import com.younike.system.service.IServerTimeService;
import com.younike.system.task.StatTask;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenpengfei
 */
@RestController("Chenpf")
@RequestMapping("demo")
@Log4j2
public class DemoController {

    @Autowired
    private IServerTimeService serverTimeService;
    @Autowired
    private IPayService payService;

    @Autowired
    private StatTask statTask;

    @Autowired
    private JwtService jwtService;

    @Autowired
    private IServerTimeBookService serverTimeBookService;

    @GetMapping(value = "/server-time-section")
    @AnonymousAccess
    public AjaxResult getServerTimeSections(@RequestParam("serverId") Long serverId, @RequestParam("goodsId") Long goodsId, @RequestParam("goodsCount") Long goodsCount) {
        if (serverId == null) {
            return AjaxResult.error("参数错误(服务人员id不能为空)");
        }
        return AjaxResult.success(serverTimeService.getServerTimeSection(serverId, goodsId, goodsCount));
    }

    @GetMapping(value = "/task")
    @MemberAccess
    public AjaxResult task() {
        statTask.merchantBizStat();
        return AjaxResult.success();
    }

    @GetMapping(value = "/member")
    @MemberAccess
    public AjaxResult member() {
        Long memberId = MemberUserContext.currentUser();
        log.info(memberId);
        return AjaxResult.success(memberId);
    }

    @GetMapping(value = "/login")
    @AnonymousAccess
    public AjaxResult login(Long id) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.LOGIN_USER_KEY, id);
        return AjaxResult.success("", jwtService.createToken(claims));
    }

    @GetMapping("/scan")
    @AnonymousAccess
    public AjaxResult scan(@RequestParam("id") String id, @RequestParam("money")BigDecimal money) {
        return AjaxResult.success("", payService.createScanOrder(id, money));
    }

    @GetMapping("/order")
    @AnonymousAccess
    public AjaxResult order(@RequestParam("serverId")Long serverId, @RequestParam("date")String date, @RequestParam("start")String start, @RequestParam("end")String end){
        // 服务人员预约时间段列表
        List<ServerTimeBook> serverTimeBookList = serverTimeBookService.selectAvailableServerTimeBookList(serverId, LocalDate.parse(date), LocalTime.parse(start), LocalTime.parse(end));
        for (ServerTimeBook serverTimeBook : serverTimeBookList) {
            if (serverTimeBook.getBooked() == 1) {
                // 预约时间段中存在已被预约时间段
                log.error("预约时间段中存在已被预约时间段，下单失败");
                throw new ServiceException("预约时间段中存在已被预约时间段，下单失败");
            }
            serverTimeBook.setBooked(1);
        }
        serverTimeBookService.updateServerTimeBooks(serverTimeBookList);
        return AjaxResult.success("", "");
    }

    @GetMapping("/cancal")
    @AnonymousAccess
    public AjaxResult cancal(@RequestParam("serverId")Long serverId, @RequestParam("date")String date, @RequestParam("start")String start, @RequestParam("end")String end){
        // 服务人员预约时间段列表
        List<ServerTimeBook> serverTimeBookList = serverTimeBookService.selectAvailableServerTimeBookList(serverId, LocalDate.parse(date), LocalTime.parse(start), LocalTime.parse(end));
        for (ServerTimeBook serverTimeBook : serverTimeBookList) {
            serverTimeBook.setBooked(0);
        }
        serverTimeBookService.updateServerTimeBooks(serverTimeBookList);
        return AjaxResult.success("", "");
    }
}
