package com.xhpc.user.api;

import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.DateUtil;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.user.constant.InvoiceMapHistoryOrderStatusConst;
import com.xhpc.user.domain.*;
import com.xhpc.user.mapper.XhpcHistoryOrderMapper;
import com.xhpc.user.pojo.XhpcInvoiceMapHistoryOrder;
import com.xhpc.user.service.XhpcInvoiceService;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 小程序的InvoiceController
 *
 * @author WH
 * @date 2021/12/23 13:49
 * @since version-1.0
 */
@RestController
@RequestMapping("/api/invoice")
public class XhpcInvoiceApiController extends BaseController {

    @Resource
    XhpcInvoiceService xhpcInvoiceService;
    @Resource
    TokenService tokenService;
    @Resource
    RedisService redisService;

    @Resource
    XhpcHistoryOrderMapper xhpcHistoryOrderMapper;

    /**
     * 查询用户显示小红点，即查询用户有几个未读的已开发票
     *
     * @author WH
     * @date 2021/12/28 18:51
     * @since version-1.0
     */
    @GetMapping("/user/no-read")
    public AjaxResult findNotReadCount(@RequestParam Long creatorId, @RequestParam Integer creatorType) {

        LoginUser loginUser = tokenService.getLoginUser();
        Long userid = loginUser.getUserid();
        String tenantId = loginUser.getTenantId();
        Integer userType = loginUser.getUserType();
        String globalInvoice="global:invoice:"+ tenantId +":";
        String invoice ="";
        if(UserTypeUtil.USER_TYPE.equals(userType)){
            invoice =globalInvoice + UserTypeUtil.USER + userid;
        }else if(UserTypeUtil.INTERNET_TYPE.equals(userType)){
            invoice =globalInvoice + UserTypeUtil.INTERNET + userid;
        }else if(UserTypeUtil.COMMUNIT_TYPE.equals(userType)){
            invoice =globalInvoice + UserTypeUtil.COMMUNIT + userid;
        }else{
            invoice =globalInvoice + UserTypeUtil.CUSTOMERS + userid;
        }
        Long notRead = xhpcInvoiceService.findNotReadCount(creatorId, creatorType);
        redisService.setCacheObject(invoice,notRead);
        return AjaxResult.success(notRead);
    }

    /**
     * 保存用户提交的开发票的信息
     *
     * @author WH
     * @date 2021/12/28 14:54
     * @since version-1.0
     */
    @Log(title = "用户要开发票", businessType = BusinessType.INSERT)
    @PostMapping("/user/commit")
    public AjaxResult saveInvoiceInfo(@RequestBody SaveInvoiceInfoRequest saveInvoiceInfoRequest) throws Exception {

        saveInvoiceInfoRequest.setCreateTime(DateUtil.getYyyyMmDdHhMmSs());
        xhpcInvoiceService.saveInvoiceInfo(saveInvoiceInfoRequest);
        return AjaxResult.success();
    }

    /**
     * 查询用户开发票的历史记录
     *
     * @author WH
     * @date 2021/12/27 12:23
     * @since version-1.0
     */
    @GetMapping(value = "/user/history")
    public AjaxResult findInvoiceHistoryRecords(InvoiceHistoryRecordsRequest invoiceHistoryRecordsRequest) {

        InvoiceHistoryRecordsResponse invoiceHistoryRecordsResponse = xhpcInvoiceService.findInvoiceHistoryRecords(invoiceHistoryRecordsRequest);
        return AjaxResult.success(invoiceHistoryRecordsResponse);
    }

    /**
     * 回显电子发票详情信息
     *
     * @param invoiceId 要回显的发票id
     * @return 返回包含了指定数据的AjaxResult
     * @author WH
     * @date 2021/12/27 13:11
     * @since version-1.0
     */
    @GetMapping(value = "/specific-info")
    public AjaxResult findSpecificInvoicedInfo(@RequestParam Long invoiceId) {

        SpecificInvoicedResponse specificInvoicedResponse = xhpcInvoiceService.findSpecificInvoicedInfo(invoiceId);
        return AjaxResult.success(specificInvoicedResponse);
    }


    /**
     * 根据用户id，用户类型，要查询的当前时间的年月日，查询该用户3个月内可以开发票的历史订单
     *
     * @author WH
     * @date 2021/12/24 14:10
     * @since version-1.0
     */
    @GetMapping(value = "/user/history-orders")
    public AjaxResult findUserHistoryOrders(HttpServletRequest request, InvoicedOrdersRequest invoicedOrdersRequest) {

        LoginUser loginUser = tokenService.getLoginUser(request);
        invoicedOrdersRequest.setTenantId(loginUser.getTenantId());
        InvoicedOrderResponse invoicedOrdersResponse = xhpcInvoiceService.findUserHistoryOrders(invoicedOrdersRequest);
        return AjaxResult.success(invoicedOrdersResponse);


    }

    /**
     * 查询发票抬头信息
     *
     * @author WH
     * @date 2021/12/23 15:07
     * @since version-1.0
     */
    @GetMapping(value = "/title-info")
    public AjaxResult findTitleInfo(@RequestParam(value = "firmName") String firmName) {

        InvoiceTitleResponse invoiceTitleResponse = xhpcInvoiceService.findTitleInfo(firmName);
        return AjaxResult.success(invoiceTitleResponse);
    }

    /**
     * 查询用户最后一次所提交的发票信息
     *
     * @author WH
     * @date 2021/12/23 15:07
     * @since version-1.0
     */
    @GetMapping(value = "/user/last-info")
    public AjaxResult findUserLastInputInfo(@RequestParam(value = "creatorId") Long creatorId, @RequestParam("creatorType") Integer creatorType) {

        TitleResponse titleResponse = xhpcInvoiceService.findUserLastInputInfo(creatorId, creatorType);
        return AjaxResult.success(titleResponse);
    }



}
