/**
 * 工料机相关控制器
 *
 * @author CaiAoLin
 * @date 2017/6/22
 * @version
 */
import BaseController from "../../common/base/base_controller";
import GLJListModel from "../models/glj_list_model";
import UnitPriceModel from "../models/unit_price_model";
import UnitPriceFileModel from "../models/unit_price_file_model";

class GLJController extends BaseController {

    /**
     * 构造函数
     *
     * @return {void}
     */
    constructor() {
        let parent = super();
        parent.title = '工料机';
    }

    /**
     * 工料机汇总页
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async index(request, response) {
        // 标段id
        let projectId = request.query.project;
        projectId = parseInt(projectId);
        try {
            if (isNaN(projectId) || projectId <= 0) {
                throw '标段id有误';
            }
            // 先获取对应标段的项目工料机数据
            let gljListModel = new GLJListModel();
            let gljList = await gljListModel.getListByProjectId(projectId);

            let renderData = {
                gljList: JSON.stringify(gljList),
                materialIdList: gljListModel.materialIdList,
                autoChangeGLJType: UnitPriceModel.autoChangeGLJType,
                triggerCalculateGLJType: UnitPriceModel.triggerCalculateGLJType
            };
            response.render('glj/html/glj_index', renderData);

        } catch (error) {
            response.status(404).send('404 Error');
        }

    }

    /**
     * 更新数据
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async updateData(request, response) {
        let field = request.body.field;
        let value = request.body.value;
        value = value === 'true' ? 1 : value;
        value = value === 'false' ? 0 : value;
        let id = request.body.id;

        let responseData = {
            err: 0,
            msg: ''
        };
        try {
            // 可编辑的字段
            let editableField = ['is_evaluate', 'unit_price.market_price', 'is_adjust_price'];
            if (editableField.indexOf(field) < 0) {
                throw '对应字段不能编辑';
            }

            let modelString = 'glj';
            // 切割字段
            let fieldArray = field.split('.');
            modelString = fieldArray.length > 1 ? fieldArray[0] : modelString;
            field = fieldArray.length > 1 ? fieldArray[1] : field;

            // 组合数据
            let updateData = new Object();
            updateData[field] = value;

            let result = false;
            let model = null;
            switch (modelString) {
                case 'glj':
                    model = new GLJListModel();
                    // 更新数据
                    result = await model.updateById(id, updateData);
                    break;
                case 'unit_price':
                    model = new UnitPriceModel();
                    // 更新数据
                    result = await model.updatePriceById(id, updateData);
                    break;
            }

            if (!result) {
                throw '更新失败';
            }
        } catch (error) {
            console.log('error:' + error);
            responseData.err = 1;
        }

        response.json(responseData);
    }

    /**
     * 模拟定额插入
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async test(request, response) {
        // 从定额库获取的数据
        let data = {
            glj_repository_id: 1,
            project_id: 1,
            code: '00010201',
            name: '土石方综合工日',
            specs: '',
            unit: '工日',
            type: 2,
            base_price: '44.76',
            market_price: '44.76'
        };
        try {
            let gljListModel = new GLJListModel();
            let result = await gljListModel.addList(data);

            console.log(result);
        } catch (error) {
            console.log(error);
        }

        response.end('success');
    }

    /**
     * 模拟定额修改单价
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async testModify(request, response) {
        let projectId = request.query.project;
        // // 修改数据
        let updateData = {
            code: '00010201',
            market_price: '49',
            name: '土石方综合工日',
            project_id: projectId
        };

        try {
            let gljListModel = new GLJListModel();
            let result = await gljListModel.modifyMarketPrice(updateData);

            console.log(result);
        } catch (error) {
            console.log(error);
        }
        response.end('success');
    }

    /**
     * 测试删除
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async delete(request, response) {
        try {
            let gljListModel = new GLJListModel();
            let result = await gljListModel.deleteById(63);

            console.log(result);
        } catch (error) {
            console.log(error);
        }
        response.end('success');
    }
}

export default GLJController;
