public with sharing class AssetNumberChangeController {
    // 基础Url
    public String baseUrl { get; private set; }
    // 样机ID
    public String astId { get; private set; }
    public Asset ast { get; private set; }
    // 样机库存调整历史
    public Loaner_change_history__c lch { get; set; }

    public AssetNumberChangeController() {
        astId = System.currentPageReference().getParameters().get('id');
    }

    public AssetNumberChangeController(ApexPages.StandardController controller) {
        astId = controller.getRecord().Id;
    }

    public PageReference init() {
        // 基础Url
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        // 样机信息取得
        if (String.isBlank(astId) == true) {
            return null;
        }
        List<Asset> astList = [select id, Name, AccountId, EC_Code__c, SerialNumber,
                                      Equipment_Type__c, lonar_owner__c, loaner_place__c, total_number__c
                                 from Asset
                                where Id = :astId];
        if (astList.size() == 0) {
            return null;
        }
        ast = astList[0];
        // 新建样机库存调整历史
        lch = new Loaner_change_history__c();

        return null;
    }

    public PageReference saveBtn() {
        // 项目check
        if (lch.change_content__c == null) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请输入调整内容。'));
            return null;
        }
        if (lch.change_number__c == null) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请输入调整数量。'));
            return null;
        }

        // 样机总数计算
        if (ast.total_number__c == null) ast.total_number__c = 0;
        Decimal total_number = ast.total_number__c + lch.change_number__c;

        Savepoint sp = Database.setSavepoint();
        try {
            // 插入样机库存调整历史
            lch.Asset__c = ast.Id;
            lch.change_day__c = Date.today();
            lch.total_number__c = total_number;
            insert lch;
            // 样机总数计算
            Asset upd_ast = new Asset(Id = ast.Id);
            upd_ast.total_number__c = total_number;
            update upd_ast;
            // 返回样机详细画面
            String url = baseUrl;
            url += '\\' + ast.Id;
            return new Pagereference(url);
        } catch (Exception ex) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage() + ' | Line:' + ex.getLineNumber()));
        }

        return null;
    }

    public PageReference cancelBtn() {
        // 返回样机详细画面
        String url = baseUrl;
        url += '\\' + ast.Id;
        return new Pagereference(url);
    }
}