package com.gree.mall.miniapp.controller.order;

import com.gree.mall.miniapp.bean.order.OrderTaxCount;
import com.gree.mall.miniapp.logic.order.OrderTaxLogic;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.annotation.ApiNotAuth;
import com.gree.mall.miniapp.bean.user.UserAddressBean;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.plus.entity.OrderTax;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@Api(value = "小程序用户发票", tags = {"小程序用户发票"})
@RequestMapping(value = "/user/order/tax", produces = "application/json; charset=utf-8")
public class OrderTaxController {

    @Autowired
    private OrderTaxLogic orderTaxLogic;

    @PostMapping("/save")
    @ApiOperation(value = "添加发票")
    public ResponseHelper<OrderTax> saveTax(
            @ApiParam(value = "object", required = true) @RequestBody OrderTax orderTaxBean,
            HttpServletRequest request
    ) throws Exception {
        log.info(orderTaxBean.toString());
        //判断纳税识别号长度
        OrderTax orderTax = orderTaxLogic.saveTax(orderTaxBean);
        return ResponseHelper.success(orderTax);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改发票")
    public ResponseHelper<OrderTax> updateTax(
            @ApiParam(value = "object", required = true) @RequestBody OrderTax orderTax
    ) throws Exception {
        orderTaxLogic.updateTax(orderTax);
        return ResponseHelper.success();
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除发票")
    public ResponseHelper<String> delTax(
            @ApiParam(required = true, value = "发票id") @RequestParam(required = true) String orderTaxId
    ) throws Exception {
        orderTaxLogic.delTax(orderTaxId);
        return ResponseHelper.success();
    }

    @PostMapping("/send")
    @ApiOperation(value = "发送发票到email")
    public ResponseHelper<String> sendTax(
            @ApiParam(required = true, value = "发票id") @RequestParam(required = true) String orderTaxId
    ) throws Exception {
        String addressId = orderTaxLogic.sendTax(orderTaxId);
        return ResponseHelper.success();
    }


    @ApiNotAuth
    @PostMapping("/yonge")
    @ApiOperation(value = "拆分邮件发送")
    public ResponseHelper yonge(
            @ApiParam(required = true, value = "excel文件") @RequestParam(required = true) MultipartFile file
    ) throws Exception {
        //orderTaxLogic.yonge(file);
        return ResponseHelper.success();
    }


    @GetMapping("/list")
    @ApiOperation(value = "发票列表")
    public ResponseHelper<Page<OrderTax>> pageTax(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(required = false, value = "true:已开,false:待开") @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws Exception {

        IPage<OrderTax> page = orderTaxLogic.page(userId,status, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/count")
    @ApiOperation(value = "发票列表")
    public ResponseHelper<OrderTaxCount> count(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId

    ) throws Exception {

        OrderTaxCount page = orderTaxLogic.count(userId);
        return ResponseHelper.success(page);
    }


    @GetMapping("/detail")
    @ApiOperation(value = "发票详情")
    public ResponseHelper<OrderTax> detail(
            @ApiParam(required = true, value = "发票id") @RequestParam(required = true) String orderTaxId
    ) throws Exception {
        OrderTax orderTax = orderTaxLogic.detail(orderTaxId);
        return ResponseHelper.success(orderTax);
    }


    @GetMapping("/title/list")
    @ApiOperation(value = "发票抬头列表")
    public ResponseHelper<String> title(
            @ApiParam(required = true, value = "公司名称") @RequestParam(required = true) String companyName
    ) throws Exception {
        //Object object = orderTaxLogic.title(companyName);
        return ResponseHelper.success();
    }


}
