package com.sunxung.factoring.web.invoicemanage;

import cn.hutool.core.collection.CollectionUtil;
import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.invoicemanage.InvoiceApproval;
import com.sunxung.factoring.entity.invoicemanage.SalesInvoiceInfo;
import com.sunxung.factoring.service.invoicemanage.InvoiceApprovalService;
import com.sunxung.factoring.service.sys.UserService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description : 销售发票审核controller层
 * @Author : JiaYf
 * @Date: 2021-10-23 14:07
 */

@Controller
@RequestMapping("/saleInvoice/approval")
public class InvoiceApprovalController {

    private final String prefix = "saleInvoice";

    @Autowired
    private InvoiceApprovalService invoiceApprovalService;

    @Autowired
    private FlowableService flowableService;
    @Autowired
    private UserService userService;


    /**
     * 跳转 发票审核页面
     * 2.0
     * @param businessKey 销售发票Id
     * @param taskId      任务Id
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model, String taskId, Long businessKey) {
        InvoiceApproval invoiceApproval = invoiceApprovalService.getFileList(businessKey);
        model.addAttribute("invoiceApproval",invoiceApproval);
        model.addAttribute("taskId", taskId);
        model.addAttribute("businessKey", businessKey);
        return prefix + "/invoiceReview";
    }

    /**
     * 财务审核
     * 2.0
     * @param invoiceApproval 采购发票审核信息
     * @return
     */
    @PostMapping("/doAudit")
    @ResponseBody
    @OperationLog(operationModule = "销售发票",operationType = OperationTypeEnum.OTHER,description = "业务、财务审核")
    public Object doAudit(@RequestBody InvoiceApproval invoiceApproval,
                           HttpServletRequest request) {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, invoiceApproval);
        //拾取task
        SalesInvoiceInfo salesInvoiceInfo = invoiceApproval.getSalesInvoiceInfo();
        String taskId = invoiceApproval.getTaskId();
        String businessKey = invoiceApproval.getBusinessKey();
        Long userId = ThreadLocalUtil.getUserId();
        List<Long> userIds = userService.findUserIdByRole(ConstantUtil.PROJECT_MANAGER);
        if(CollectionUtil.isNotEmpty(userIds) && userIds.contains(userId)){
            flowableService.claimTask(taskId, userId, ConstantUtil.PROJECT_MANAGER);
        }else {
            flowableService.claimTask(taskId, userId, ConstantUtil.CASHIER);
        }
        invoiceApprovalService.audit(invoiceApproval, businessKey, taskId, request);
        return new ResponseJson("审核完成");
    }

    /**
     * 跳转 发票审核页面
     * 2.0
     * @param businessKey 销售发票Id
     * @param taskId      任务Id
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public Object add(@RequestParam String taskId,@RequestParam Long businessKey) {
        InvoiceApproval invoiceApproval = invoiceApprovalService.getFileList(businessKey);
        return new ResponseJson(invoiceApproval);
    }
}
