package com.dayou.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dayou.dto.ItemAddFinanceInvoiceDTO;
import com.dayou.dto.MajorProductionSimpleDTO;
import com.dayou.vo.FinanceInvoiceApplyExportVo;
import com.dayou.vo.FinanceInvoiceDestroyExportVo;
import com.dayou.vo.FinanceInvoiceRealExportVo;
import com.dayou.vo.FinanceInvoiceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IFinanceInvoiceService;
import com.dayou.entity.FinanceInvoice;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 财务开票
 *
 * @author wucl
 * @since 2023-12-08
 */
@RestController
@RequestMapping("financeInvoice")
@Slf4j
public class FinanceInvoiceController extends BaseController {
    @Autowired
    private IFinanceInvoiceService financeInvoiceService;

    /**
    * 财务开票列表
    */
    @GetMapping("")
    public RestResponse<Page<FinanceInvoiceVo>> page(FinanceInvoice financeInvoice, Page page,String keyword){
        Page<FinanceInvoiceVo> pages=financeInvoiceService.selectPage(page,financeInvoice,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 财务开票列表导出
     */
    @GetMapping("/export")
    public void page(FinanceInvoice financeInvoice, String keyword, HttpServletResponse response) throws IOException {
        List<FinanceInvoiceVo> result =financeInvoiceService.exportPage(financeInvoice, keyword);
        // 不同状态要导出的字段不一样,处理一下
        switch (financeInvoice.getState()) {
            case "审核中": {
                List<FinanceInvoiceApplyExportVo> list = new ArrayList<>();
                for (FinanceInvoiceVo financeInvoiceVo : result) {
                    list.add(BeanUtil.toBean(financeInvoiceVo, FinanceInvoiceApplyExportVo.class));
                }
                exportPlus(response, financeInvoice.getState() + "发票", list, FinanceInvoiceApplyExportVo.class);
                break;
            }
            case "已开票": {
                List<FinanceInvoiceRealExportVo> list = new ArrayList<>();
                for (FinanceInvoiceVo financeInvoiceVo : result) {
                    list.add(BeanUtil.toBean(financeInvoiceVo, FinanceInvoiceRealExportVo.class));
                }
                exportPlus(response, financeInvoice.getState() + "发票", list, FinanceInvoiceRealExportVo.class);
                break;
            }
            case "已作废": {
                List<FinanceInvoiceDestroyExportVo> list = new ArrayList<>();
                for (FinanceInvoiceVo financeInvoiceVo : result) {
                    list.add(BeanUtil.toBean(financeInvoiceVo, FinanceInvoiceDestroyExportVo.class));
                }
                exportPlus(response, financeInvoice.getState() + "发票", list, FinanceInvoiceDestroyExportVo.class);
                break;
            }
        }
    }

    /**
     * 财务开票详情
     */
    @GetMapping("/{id}")
    public RestResponse<FinanceInvoice> detail(@PathVariable Long id){
        FinanceInvoice xFinanceInvoice =financeInvoiceService.detail(id);
        return RestResponse.data(xFinanceInvoice);
     }

    /**
     * 财务开票新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody FinanceInvoice financeInvoice) {
        Boolean ret = financeInvoiceService.add(financeInvoice);
        return RestResponse.data(ret);
    }

    /**
     * 土规项目财务开票新增
     */
    @PostMapping("item/add")
    public RestResponse<Long> save(@RequestBody ItemAddFinanceInvoiceDTO dto) {
        return RestResponse.data(financeInvoiceService.itemAdd(dto));
    }

    /**
     * 财务开票更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody FinanceInvoice financeInvoice) {
        Boolean ret = financeInvoiceService.update(financeInvoice);
        return RestResponse.data(ret);
    }

    /**
     * 开票
     * @param financeInvoice
     * @return
     */
    @PutMapping("/check")
    public RestResponse<Boolean> invoiceCheck(@RequestBody FinanceInvoice financeInvoice) {
        Boolean ret = financeInvoiceService.invoiceCheck(financeInvoice);
        return RestResponse.data(ret);
    }

    /**
     * 财务开票删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = financeInvoiceService.delete(id);
        return RestResponse.data(ret);
    }

    /**
     * 单个项目开票列表
     * @param id
     * @return
     */
    @GetMapping("/fund/{id}/{productionFundId}")
    public RestResponse<List<FinanceInvoice>> getList(@PathVariable("id") Long id, @PathVariable("productionFundId") Long productionFundId){
        List<FinanceInvoice> result = financeInvoiceService.getList(id, productionFundId);
        return RestResponse.data(result);
    }

    /**
     * 开票作废申请
     * @param financeInvoice
     * @return
     */
    @PutMapping("/cancellation")
    public RestResponse<Boolean> cancellation(@RequestBody FinanceInvoice financeInvoice){
        Boolean ret = financeInvoiceService.cancellation(financeInvoice);
        return RestResponse.data(ret);
    }

    /**
     * 开票作废
     * @param id
     * @return
     */
    @GetMapping("/cancelling/{id}")
    public RestResponse<Boolean> cancelling(@PathVariable("id") Long id){
        Boolean ret = financeInvoiceService.cancelling(id);
        return RestResponse.data(ret);
    }

    /**
     * 收款开票信息
     * @param simpleDTO
     * @return
     */
    @PostMapping("/productions")
    public RestResponse<List<FinanceInvoiceVo>> getInvoiceAmountInfo(@RequestBody MajorProductionSimpleDTO simpleDTO){
        List<FinanceInvoiceVo> ret = financeInvoiceService.getInvoiceAmountInfo(simpleDTO);
        return RestResponse.data(ret);
    }

}

