package cn.superdesk.uniorder.controller.accounting.operator;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.accounting.unified.operator.AccountingUnifiedOperatorQueryParam;
import cn.superdesk.uniorder.dto.vo.accounting.unified.operator.AccountingBillVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.operator.AccountingBillBaseVo;
import cn.superdesk.uniorder.dto.vo.accounting.unified.operator.AccountingUnifiedOperatorVo;
import cn.superdesk.uniorder.dubbo.consumer.accounting.operator.AccountingUnifiedOperatorDubboConsumerService;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Api(value = "[对账单]-运营商-对账单", tags = {"运营商对账单"})
@Slf4j
@RestController
@RequestMapping("/accounting/unified/operator")
@RequiredArgsConstructor
public class AccountingUnifiedOperatorContoller extends BaseController {

    private final AccountingUnifiedOperatorDubboConsumerService accountingUnifiedOperatorDubboService;

    /**
     * 对账任务
     */
    @PostMapping("/getAccountingTaskList")
    public R<List<AccountingUnifiedOperatorVo>> getAccountingTaskList(@RequestBody @Validated AccountingUnifiedOperatorQueryParam param) {
        param.setAccountingType(1);
        log.info("对账任务列表：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.getList(param);
    }

    /**
     * 账单管理
     */
    @PostMapping("/getAccountingList")
    public R<List<AccountingUnifiedOperatorVo>> getAccountingList(@RequestBody @Validated AccountingUnifiedOperatorQueryParam param) {
        param.setAccountingType(2);
        log.info("账单管理列表：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.getList(param);
    }

    /**
     * 根据id查询
     */
    @PostMapping("/get/{id}")
    public R<AccountingBillVo> get(@PathVariable("id") Long id) {
        log.info("账单管理详情--->入参：id:{}", id);
        return accountingUnifiedOperatorDubboService.getById(id);
    }

    /**
     * 账单管理-导出列表
     * @param param
     * @param response
     */
    @PostMapping("/exportList")
    public R<String>  exportList(@RequestBody AccountingUnifiedOperatorQueryParam param,HttpServletResponse response){
        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(getToken());
        if(null != loginSysUser){
            param.setOperator(loginSysUser.getFullName());
            param.setOperatorId(loginSysUser.getSysUserId());
        }
        param.setAccountingType(2);
        log.info("账单管理-导出列表：入参：param:{}", JSON.toJSONString(param));
        return new R<>(accountingUnifiedOperatorDubboService.exportList(param,loginSysUser));

    }

    /**
     * 账单管理-导出账单    生成的地方由待提交那一步时生成，然后把地址回填到账单里面，这里直接给地址
     * @param param
     * @param response
     */
    @PostMapping("/exportAccountList")
    public void exportAccountList(@RequestBody AccountingUnifiedOperatorQueryParam param,HttpServletResponse response){
        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(getToken());
        if(null != loginSysUser){
            param.setOperator(loginSysUser.getFullName());
            param.setOperatorId(loginSysUser.getSysUserId());
        }
        param.setAccountingType(2);
        log.info("账单管理-导出账单：入参：param:{}", JSON.toJSONString(param));
        accountingUnifiedOperatorDubboService.exportAccountList(param,loginSysUser);
    }

    /**
     * 账单管理-导出账单(含供应商成本)   用固定模板，不需要根据结算方案判断
     * @param param
     * @param response
     */
    @PostMapping("/exportAccountSupplierList")
    public void exportAccountSupplierList(@RequestBody AccountingUnifiedOperatorQueryParam param,HttpServletResponse response){
        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(getToken());
        if(null != loginSysUser){
            param.setOperator(loginSysUser.getFullName());
            param.setOperatorId(loginSysUser.getSysUserId());
        }
        param.setAccountingType(2);
        log.info("账单管理-导出账单(含供应商成本)：入参：param:{}", JSON.toJSONString(param));
        accountingUnifiedOperatorDubboService.exportAccountSupplierList(param,loginSysUser);

    }


    /**
     * 账单基础信息
     */
    @PostMapping("/baseInfo")
    public R<AccountingBillBaseVo> baseInfo(@RequestBody AccountingUnifiedOperatorQueryParam param){

        log.info("账单管理-确认账单(账单管理)：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.getBaseBillInfo(param);

    }


    /**
     * 确认账单
     */
    @PostMapping("/confirmBill")
    public R<String> confirmBill(@RequestBody AccountingUnifiedOperatorQueryParam param){

        log.info("账单管理-确认账单(账单管理)：入参：param:{}", JSON.toJSONString(param));
       return accountingUnifiedOperatorDubboService.confirmBill(param,getToken());

    }

    /**
     * 取消账单  - 重新对账
     */
    @PostMapping("/cancelBill")
    public R<String> cancelBill(@RequestBody AccountingUnifiedOperatorQueryParam param){
        if (StringUtils.isBlank(param.getReason())) {
            return new R<>("500", "取消原因不能为空", false);
        }
        log.info("账单管理-确认账单(账单管理)：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.cancelBill(param,getToken());

    }

    /**
     * 驳回
     */
    @PostMapping("/rejectBill")
    public R<String> rejectBill(@RequestBody AccountingUnifiedOperatorQueryParam param){
        if (StringUtils.isBlank(param.getReason())) {
            return new R<>("500", "驳回原因不能为空", false);
        }
        log.info("账单管理-确认账单(账单管理)：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.rejectBill(param,getToken());

    }
    /**
     * 同意驳回
     */
    @PostMapping("/agreeReject")
    public R<String> agreeReject(@RequestBody AccountingUnifiedOperatorQueryParam param){

        log.info("账单管理-同意驳回(账单管理)：入参：param:{}", JSON.toJSONString(param));
        return accountingUnifiedOperatorDubboService.agreeReject(param,getToken());

    }

    /**
     * 备注
     */
    @PostMapping("/saveRemark")
    public R<String> saveRemark(@RequestBody AccountingUnifiedOperatorQueryParam param){
        if (StringUtils.isNotBlank(param.getRemark()) && param.getRemark().length() <= 200) {
            log.info("账单管理-备注(账单管理)：入参：param:{}", JSON.toJSONString(param));
            return accountingUnifiedOperatorDubboService.saveRemark(param, getToken());
        }

        return new R<>("500", "备注不能为空,并且长度不能超过200字", false);
    }




}
