package com.cyksj.web.controller.manage.invoice;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.InvoiceCompanyMapper;
import com.cyksj.mapper.UserInvoiceApplyRecordMapper;
import com.cyksj.model.entity.InvoiceCompany;
import com.cyksj.model.entity.UserInvoiceApplyRecord;
import com.cyksj.model.excel.ExcelInvoiceData;
import com.cyksj.model.views.UserInvoiceApplyRecordView;
import com.cyksj.web.util.EasyExcelUtils;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * admin/发票管理
 * 项目名: yhlxj
 * 文件名: UserInvoiceController
 * 创建者: JavaZou
 * 创建时间:2024/12/13 15:43
 */
@RestController
@RequestMapping("/manage/invoice")
@RequiredArgsConstructor
public class UserInvoiceController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final UserInvoiceApplyRecordMapper userInvoiceApplyRecordMapper;

	private final InvoiceCompanyMapper invoiceCompanyMapper;

	/**
	 * 用户发票申请记录
	 */
	@GetMapping("/get")
	public Result<SearchResult<UserInvoiceApplyRecordView>> get() {
		SearchResult<UserInvoiceApplyRecordView> search = beanSearcher.search(UserInvoiceApplyRecordView.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(UserInvoiceApplyRecordView::getUpdateTime).desc().build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 配置发票
	 */
	@PutMapping("/set/invoiceImg")
	public Result<String> setInvoiceImg(@RequestBody UserInvoiceApplyRecord userInvoiceApplyRecord) {
		String invoiceImg = userInvoiceApplyRecord.getInvoiceImg();
		Long id = userInvoiceApplyRecord.getId();
		UserInvoiceApplyRecord invoiceApplyRecord = userInvoiceApplyRecordMapper.selectById(id);
		if (invoiceApplyRecord.getStatus() == 2) {
			throw BusinessRuntimeException.getInstance("发票已发送");
		}
		invoiceApplyRecord.setFileName(userInvoiceApplyRecord.getFileName());
		invoiceApplyRecord.setStatus(1);
		invoiceApplyRecord.setInvoiceImg(invoiceImg);
		userInvoiceApplyRecordMapper.updateById(invoiceApplyRecord);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 导出发票
	 */
	@GetMapping("/export")
	public void exportInvoice(HttpServletResponse response) {
		List<ExcelInvoiceData> list = beanSearcher.searchAll(ExcelInvoiceData.class, MapUtils.flatBuilder(request.getParameterMap()).orderBy(ExcelInvoiceData::getUpdateTime).desc().build());
		EasyExcelUtils.createExcelStreamMutilByEasyExcel(response, ExcelInvoiceData.class, list, "用户发票", "用户发票", ExcelTypeEnum.XLSX, null);
	}

	//==========================================================税率管理=================================================================================

	/**
	 * 税率管理列表
	 */
	@GetMapping("/get/company/taxRate")
	public Result<List<InvoiceCompany>> getCompanyTaxRate() {
		List<InvoiceCompany> invoiceCompanies = beanSearcher.searchAll(InvoiceCompany.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(invoiceCompanies);
	}

	/**
	 * 新增公司税率
	 */
	@PostMapping("/post/company/taxRate")
	public Result<String> insertCompanyTaxRate(@RequestBody InvoiceCompany invoiceCompany) {
		invoiceCompanyMapper.insert(invoiceCompany);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑公司税率
	 */
	@PutMapping("/update/company/taxRate")
	public Result<String> updateCompanyTaxRate(@RequestBody InvoiceCompany invoiceCompany) {
		invoiceCompanyMapper.updateById(invoiceCompany);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除公司税率
	 */
	@DeleteMapping("/delete/company/taxRate/{id}")
	public Result<String> updateCompanyTaxRate(@PathVariable Long id) {
		invoiceCompanyMapper.deleteById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
