package com.goafanti.order.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.*;
import com.goafanti.order.enums.OrderLegalAffairsState;
import com.goafanti.order.service.LegalAffairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/api/admin/lagalAffairs")
public class LegalAffairsApiController extends CertifyApiController {
    @Autowired
    private LegalAffairsService legalAffairsService;



    /**
     * 订单催款转交法务
     * @param orderNo 订单编号
     * @return 成功 1
     */
    @RequestMapping(value = "/transfer")
    public Result transferLagalAffairs(String orderNo,String remarks){
        Result res =new Result();
        if (orderNo==null){
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"订单编号"));
            return res;
        }
        if (legalAffairsService.checkTask(orderNo)){
            res.getError().add(buildError("高新项目未完成，请先完成项目！","高新项目未完成，请先完成项目！"));
            return res;
        }
        res.data(legalAffairsService.pushTransferLagalAffairs(orderNo,remarks));
        return res;

    }

    /**
     * 订单催款列表（法务）
     * @param in
     * @return list
     */
    @RequestMapping(value = "/orderList",method = RequestMethod.GET)
    public Result orderList(InputOrderListBo in){
        Result res =new Result();
        res.data(legalAffairsService.orderListPage(in));
        return res;
    }

    /**
     * 订单催款列表（法务）
     * @param in
     * @return
     */
    @RequestMapping(value = "/orderList/Exprot",method = RequestMethod.GET)
    public Result orderListExprot(InputOrderListBo in){
        return legalAffairsService.orderListExprot(in);
    }
    /**
     * 订单催款日志列表（法务）
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/dunOrderList",method = RequestMethod.GET)
    public Result dunOrderList(String orderNo){
        Result res = new Result();
        if (StringUtils.isBlank(orderNo)){
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"订单编号"));
            return res;
        }
        return res.data(legalAffairsService.dunOrderList(orderNo));
    }

    /**
     * 订单催款日志列表（法务）
     * @param in
     * @return
     */
    @RequestMapping(value = "/addDunOrder",method = RequestMethod.POST)
    public Result addDunOrder(@Valid InputDunOrder in, BindingResult bindingResult){
        Result res = new Result();
        if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
                    "订单编号、日期、备注"));
            return res;
        }
        return res.data(legalAffairsService.addDunOrder(in));
    }


    /**
     * 新增法务记录
     * @param in
     * @return
     */
    @RequestMapping(value = "/addLagalAffairsLog",method = RequestMethod.POST)
    public Result addLagalAffairsLog(@Valid InputOrderLegalAffairs in,BindingResult bindingResult){
        Result res =new Result();
        if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
                    OrderLegalAffairsState.getFieldDesc(bindingResult.getFieldError().getField())));
            return res;
        }
        res.data(legalAffairsService.addLagalAffairsLog(in));
        return res;
    }

    /**
     * 法务记录列表
     * @return
     */
    @RequestMapping(value = "/lagalAffairsLogList",method = RequestMethod.GET)
    public Result lagalAffairsLogList(String orderNo){
        Result res =new Result();
        if (orderNo==null){
            res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,ErrorConstants.PARAM_EMPTY_ERROR,"订单编号"));
        }
        res.data(legalAffairsService.lagalAffairsLogList(orderNo));
        return res;
    }

    /**
     * 法务日志文件上传
     */
    @RequestMapping(value = "/uploadLegalLogFile", method = RequestMethod.POST)
    public Result uploadDunLogFileResult(HttpServletRequest req, String sign) {
        Result res = new Result();
        AttachmentType attachmentType = AttachmentType.getField(sign);
        if (attachmentType == AttachmentType.LEGAL_LOG_ATTACHMENT) {
            res.setData(handleFiles(res, "/legal_log_attachment/", false, req, sign, TokenManager.getAdminId()));
        } else {
            res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
        }
        return res;
    }
}
