package com.sunxung.factoring.web.invoicemanage;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.customerinformation.Customer;
import com.sunxung.factoring.entity.invoicemanage.InvoiceBasicInfo;
import com.sunxung.factoring.entity.invoicemanage.vo.SearchGoodsVo;
import com.sunxung.factoring.entity.settlement.SettlementGoodsInfo;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.invoicemanage.InvoiceBasicInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description : 销售发票控制层
 * @Author : 刘涛
 * @Date: 2022-3-24
 */
@Controller
@RequestMapping("/saleInvoice")
public class InvoiceController {

    private final String prefix = "saleInvoice";

    @Resource
    private CustomerService customerService;

    @Resource
    private InvoiceBasicInfoService invoiceBasicInfoService;

    /**
     * 初始化参数数组长度最大值
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setAutoGrowNestedPaths(true);
        binder.setAutoGrowCollectionLimit(1024);
    }


    /**
     * 跳转我已处理页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("/processedList")
    public String processedList(Long businessBasicId, Model model) {
        model.addAttribute("businessBasicId", businessBasicId);
        return prefix + "/processedList";
    }

    /**
     * 跳转-销售发票-待我处理页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("/pendingSaleList")
    public String pendingSaleList(Long businessBasicId, Model model) {
        model.addAttribute("businessBasicId", businessBasicId);
        return prefix + "/pendingList";
    }

    /**
     * 跳转全部页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("/allList")
    public String allList(Long businessBasicId, Model model) {
        model.addAttribute("businessBasicId", businessBasicId);
        return prefix + "/allList";
    }

    /**
     * 跳转 销售发票--添加发票页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("/addSaleInvoice")
    public String add(Model model) {
        List<Customer> customers = customerService.getCustomersByUser();
        model.addAttribute("customers", customers);
        return prefix + "/invoiceEntry";
    }

    /**
     * 保存业务录入销售发票信息
     * 2.0
     *
     * @param invoiceBasicInfo 销售发票添加信息
     * @return
     */
    @RequestMapping("/doAdd")
    @ResponseBody
    @OperationLog(operationModule = "销售发票",operationType = OperationTypeEnum.INSERT,description = "保存业务录入销售发票信息")
    public Object doAdd(@RequestBody InvoiceBasicInfo invoiceBasicInfo, HttpServletRequest request) throws IOException {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, invoiceBasicInfo);
        invoiceBasicInfoService.add(invoiceBasicInfo, request);
        return new ResponseJson("发票录入完成");
    }

    /**
     * 跳转财务退回业务修改页面
     * 2.0
     *
     * @param businessKey 销售发票信息ID
     * @return
     */
    @RequestMapping("/add")
    public String update(String taskId, Long businessKey, Model model) {
        InvoiceBasicInfo invoiceBasicInfo = invoiceBasicInfoService.getUpdateDetail(businessKey);
        model.addAttribute("invoiceBasicInfo", invoiceBasicInfo);
        List<Customer> customers = customerService.getCustomersByUser();
        model.addAttribute("taskId", taskId);
        model.addAttribute("businessKey", businessKey);
        model.addAttribute("customers", customers);
        return prefix + "/invoiceUpdate";
    }

    /**
     * 更新销售发票
     * 2.0
     *
     * @param invoiceBasicInfo
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/doUpdate")
    @ResponseBody
    @OperationLog(operationModule = "销售发票",operationType = OperationTypeEnum.UPDATE,description = "更新销售发票")
    public Object doUpdate(InvoiceBasicInfo invoiceBasicInfo, String taskId, HttpServletRequest request) throws IOException {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.UPDATE, invoiceBasicInfo);
        invoiceBasicInfoService.doUpdate(invoiceBasicInfo, taskId, request);
        return new ResponseJson("销售发票更新完成!");
    }

    /**
     * 更新销售发票 -C端调用接口
     * 2.0
     *
     * @param invoiceBasicInfo
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/c_doUpdate")
    @ResponseBody
    @OperationLog(operationModule = "销售发票",operationType = OperationTypeEnum.UPDATE,description = "更新销售发票")
    public Object do_Update(@RequestBody InvoiceBasicInfo invoiceBasicInfo,  HttpServletRequest request) throws IOException {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.UPDATE, invoiceBasicInfo);
        invoiceBasicInfoService.c_doUpdate(invoiceBasicInfo, invoiceBasicInfo.getTaskId(), request);
        return new ResponseJson("销售发票更新完成!");
    }

    /**
     * 跳转查看页面
     * 2.0
     */
    @RequestMapping("/view")
    public String viewSaleInvoice(Long historyId, Long viewInitiation) {
        return prefix + "/basicView";
    }

    /**
     * 跳转到历史页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("/history")
    public String history() {
        return prefix + "/history";
    }

    /**
     * 获取销售发票金额类型为：货款的货物信息
     * 2.0
     *
     * @param
     * @return
     */
    @GetMapping("/saleSelectGoodsOfPayment")
    @ResponseBody
    public GridPage<SettlementGoodsInfo> saleSelectGoodsOfPayment(SearchGoodsVo searchGoodsVo) {
        return invoiceBasicInfoService.saleSelectGoodsOfPayment(searchGoodsVo);
    }

    /**
     * 获取采购发票金额类型为：贴现费用的货物信息
     * 2.0
     *
     * @param searchGoodsVo
     * @return
     */
    @GetMapping("/saleSelectGoodsOfDiscount")
    @ResponseBody
    public GridPage<SettlementGoodsInfo> saleSelectGoodsOfDiscount(SearchGoodsVo searchGoodsVo) {
        return invoiceBasicInfoService.saleSelectGoodsOfDiscount(searchGoodsVo);
    }

    /**
     * 获取该笔业务对应的开票信息
     * 2.0
     *
     * @param customerId
     * @return
     */
    @GetMapping("/findCustomer")
    @ResponseBody
    public ResponseJson findCustomer(Long customerId, Long businessBasicInfoId) {
        return new ResponseJson(invoiceBasicInfoService.findByCustomer(customerId, businessBasicInfoId));
    }

    /**
     * 一键下载
     * @param id
     * @param response
     */
    @GetMapping("/downloadAllInvoiceFile")
    @OperationLog(operationModule = "销售发票",operationType = OperationTypeEnum.OTHER,description = "一键下载")
    public void downloadAllInvoiceFile(Long id, HttpServletResponse response) {
        invoiceBasicInfoService.downloadAllInvoiceFile(id, response);
    }

}
