package com.awspaas.user.apps.donenow_ctt.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.Logger;
import com.awspaas.user.apps.donenow_ctt.cttConstant;
import com.awspaas.user.apps.donenow_ctt.service.contractService;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class contractApproveController {
    private static final Logger contractLogger = SDK.getLogAPI().getLogger(contractApproveController.class);//记录日志

    /**
     * 定期服务审批
     * @param uc
     * @param approveDate
     * @param approveIds
     * @return
     */
    @Mapping(value = "com.awspaas.user.apps.donenow_ctt.approve_service")
    public ResponseObject approveService(UserContext uc, String approveDate, String approveIds) throws SQLException {
        if (StringUtils.isBlank(approveDate) || StringUtils.isBlank(approveIds))
            return ResponseObject.newErrResponse("参数错误");

        contractService.getInstance().postRecurringServices(uc, approveDate, approveIds);

        return ResponseObject.newOkResponse();
    }

    /**
     * 里程碑审批
     * @param uc
     * @param approveDate
     * @param approveIds
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.approve_milestone")
    public ResponseObject approveMilestone(UserContext uc, String approveDate, String approveIds) throws SQLException {
        if (StringUtils.isBlank(approveDate) || StringUtils.isBlank(approveIds))
            return ResponseObject.newErrResponse("参数错误");

        contractService.getInstance().postMilestone(uc, approveDate, approveIds);
        return ResponseObject.newOkResponse();
    }

    /**
     * / 调整订阅周期、定期服务（服务、服务调整、服务包、服务包调整、初始费用）、成本的计费总价
     * @param uc
     * @param periodPrice
     * @param id
     * @param type
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.approve_updateExtendedPrice")
    public ResponseObject updateExtendedPrice(UserContext uc, Double periodPrice, String id, int type) throws SQLException {
        if (type == cttConstant.APPROVE_SUBSCRIPTIONS)//订阅
        {

        } else if (type == cttConstant.APPROVE_RECURRING_SERVICES)//定期服务审批
        {

            BO ccBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT", id);//合同
            if (ccBO != null) {
                ccBO.set("ADJUST_SETUP_FEE", periodPrice);//调整后合同初始费用
                SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT", ccBO);
            } else {
                BO ccsaBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_ADJUST", id);//定期服务调整
                if (ccsaBO != null) {
                    ccsaBO.set("ADJUST_PRORATED_PRICE_CHANGE", periodPrice);//调整后合同初始费用
                    SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_SERVICE_ADJUST", ccsaBO);
                } else {
                    BO ccspBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD", id);//周期服务
                    if (ccspBO != null) {
                        ccspBO.set("PERIOD_ADJUSTED_PRICE", periodPrice);//调整后合同初始费用
                        SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD", ccspBO);
                    }
                }
            }
        }

        return ResponseObject.newOkResponse();
    }

    /**
     * 审批初始费用
     * @param uc
     * @param ids
     * @param type
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.approve_restoreInitialPrice")
    public ResponseObject restoreInitialPrice(UserContext uc, String ids, int type) throws SQLException {
        if (type == cttConstant.APPROVE_SUBSCRIPTIONS)//订阅
        {

        } else if (type == cttConstant.APPROVE_RECURRING_SERVICES)//定期服务审批
        {
            for (String id : ids.split(",")) {

                BO ccBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT", id);//合同
                if (ccBO != null) {
                    ccBO.set("ADJUST_SETUP_FEE", ccBO.get("SETUP_FEE"));//调整后合同初始费用
                    SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT", ccBO);
                } else {
                    BO ccsaBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_ADJUST", id);//定期服务调整
                    if (ccsaBO != null) {
                        ccsaBO.set("ADJUST_PRORATED_PRICE_CHANGE", ccsaBO.get("PRORATED_PRICE_CHANGE"));//调整后合同初始费用
                        SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_SERVICE_ADJUST", ccsaBO);
                    } else {
                        BO ccspBO = SDK.getBOAPI().get("BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD", id);//周期服务
                        if (ccspBO != null) {
                            ccspBO.set("PERIOD_ADJUSTED_PRICE", ccspBO.get("PERIOD_PRICE"));//调整后合同初始费用
                            SDK.getBOAPI().update("BO_EU_DNCTT_CONTRACT_SERVICE_PERIOD", ccspBO);
                        }
                    }
                }
            }
        }
        return ResponseObject.newOkResponse();
    }

    /**
     * 审批合同产品
     * @param uc
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.approve_cost")
    public ResponseObject cost(UserContext uc, String approveDate, String approveIds) throws SQLException {

        boolean result = contractService.getInstance().postCost(uc, approveDate, approveIds);

        return result ? ResponseObject.newOkResponse() : ResponseObject.newErrResponse("审批失败");
    }

    /**
     * 撤销定期服务
     * @param uc
     * @param ids
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.revoke_recurring_services")
    public ResponseObject revokeRecurringServices(UserContext uc, String ids) throws SQLException {
        String result = contractService.getInstance().revokeRecurringServices(uc, ids);
        if (StringUtils.isNotBlank(result)) return ResponseObject.newErrResponse(result);

        contractLogger.info("撤销定期服务成功");
        return ResponseObject.newOkResponse();
    }

    /**
     * 撤销里程碑审批
     * @param uc
     * @param ids
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.revoke_milestones")
    public ResponseObject revokeMilestones(UserContext uc, String ids) throws SQLException {
        String result = contractService.getInstance().revokeMilestones(uc, ids);
        if (StringUtils.isNotBlank(result)) return ResponseObject.newErrResponse(result);

        contractLogger.info("撤销里程碑成功");
        return ResponseObject.newOkResponse();
    }

    @Mapping("com.awspaas.user.apps.donenow_ctt.revoke_cost")
    public ResponseObject revokeCost(UserContext uc, String ids) throws SQLException {
        String result = contractService.getInstance().revokeCost(uc, ids);
        if (StringUtils.isNotBlank(result)) return ResponseObject.newErrResponse(result);

        contractLogger.info("撤销合同成本成功");
        return ResponseObject.newOkResponse();
    }

    /**
     * 修改税
     * @param uc
     * @param id
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.modify_taxRate")
    public ResponseObject modifyTaxRate(UserContext uc, String id) throws SQLException {

        BO bo = SDK.getBOAPI().get("BO_EU_DNCRM_ACCOUNT_DEDUCTION", id);

        String account_id = bo.getString("ACCOUNT_ID");
        String extended_price = bo.getString("EXTENDED_PRICE");
        String tax_category_id = bo.getString("TAX_CATEGORY_ID");
        String effective_tax_rate = bo.getString("EFFECTIVE_TAX_RATE");
        String tax_dollars = bo.getString("TAX_DOLLARS");

        List<RowMap> effectiveTaxRateList = DBSql.getMaps("select tax_cate_id as taxCateId,total_effective_tax_rate as effectiveTaxRate from bo_eu_dnd_tax_region_cate where tax_region_id IN (select tax_region_id from bo_eu_dncrm_account where id=?)", new Object[]{account_id});

        List<RowMap> taxCateList = DBSql.getMaps("SELECT oid,NAME FROM bo_eu_dnd_general WHERE general_table_id=44 AND CLOSED=0 AND ORGID=? AND oid in(select tax_cate_id from bo_eu_dnd_tax_region_cate where tax_region_id in (select tax_region_id from bo_eu_dncrm_account where id=?))", new Object[]{uc.getCompanyModel().getId(), account_id});

        Map<String, Object> map = new HashMap<>();
        map.put("taxCateList", taxCateList);
        map.put("effectiveTaxRateList", effectiveTaxRateList);

        map.put("taxCateId", tax_category_id);
        map.put("effectiveTaxRate", effective_tax_rate);
        map.put("taxDollars", tax_dollars);
        map.put("extendedPrice", extended_price);

        ResponseObject responseObject = ResponseObject.newOkResponse();
        responseObject.setData(map);

        return responseObject;
    }

    /**
     * 修改税保存
     * @param uc
     * @param id
     * @param taxCateId
     * @param effectiveTaxRate
     * @param taxDollars
     * @return
     * @throws SQLException
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.modify_taxRate_save")
    public ResponseObject modifyTaxRateSave(UserContext uc, String id, String taxCateId, String effectiveTaxRate, String taxDollars) throws SQLException {
        BO bo = SDK.getBOAPI().get("BO_EU_DNCRM_ACCOUNT_DEDUCTION", id);
        bo.set("TAX_CATEGORY_ID", taxCateId);
        bo.set("tax_category_name", DBSql.getString("SELECT NAME FROM bo_eu_dnd_general WHERE general_table_id=44 AND oid=?", new Object[]{taxCateId}));
        bo.set("EFFECTIVE_TAX_RATE", effectiveTaxRate);
        bo.set("TAX_DOLLARS", taxDollars);
        SDK.getBOAPI().update("BO_EU_DNCRM_ACCOUNT_DEDUCTION", bo);
        return ResponseObject.newOkResponse();
    }

}
