/**
 * 农资
 */

import moment = require("moment");
import { TABLENAME } from "../../config/dbEnum";
import { NongZiConfig } from "../../config/eccParam";
import { NONGZITYPE, PLANTTYPE, PURPOSE } from "../../config/enum";
import * as nongziData from "../../data/nongzi";
import * as dikuaiData from "../../data/dikuai";
import { randomId, successResult } from "../../tools/system";
import { changeEnumValue, eccEnumValue } from "../../util/verificationEnum";
import { eccFormParam } from "../../util/verificationParam";
import { BizError } from "../../util/bizError";
import { ERRORENUM } from "../../config/errorEnum";


/**
 * 添加农资
 * @param reqUser 
 * @param param 
 * @returns 
 */
export async function addNongZi(reqUser, param) {
    let funName = `添加农资`;
    eccFormParam(funName, NongZiConfig, param );
    eccEnumValue(funName, "nzType", NONGZITYPE, param.nzType);
    eccEnumValue(funName, "plantType", PLANTTYPE, param.plantType);
    //确保添加时地块还在
    let dikuaiList = await dikuaiData.selectToParam({});
    let diKuaiMap = {};
    dikuaiList.forEach(info => {
        let {dId, code} = info;
        diKuaiMap[dId] = code;
    });
    if (!diKuaiMap[param.dIdList]) throw new BizError(ERRORENUM.地块不存在, param.dIdList);

    let addInfo = {
        nzId:randomId(TABLENAME.农资),
        nzType:param.nzType,
        dIdList:[param.dIdList],
        plantType:param.plantType,
        count:param.count,
        useTime:param.useTime,
        ct:new Date().valueOf()
    };
    await nongziData.addData(addInfo)

    return successResult();
}


/**
 * 农资列表
 * @param selectStr 
 * @param dId 
 * @param nzType 
 * @param useTime 
 * @returns 
 */
export async function nongZiList(selectStr:string, dId:string, nzType:number, useTime:number) {
    let funName = "农资列表";
    let param:any = {};
    if (nzType) {
        eccEnumValue(funName, "nzType", NONGZITYPE, nzType);
        param.nzType = nzType;
    }
    if (dId) {
        param.dIdList = {"$in":dId};
    }
    if (useTime) {
        let startMs = moment(useTime).startOf("day").valueOf();
        let endMs = moment(useTime).endOf("day").valueOf();
        param.useTime = {"$gte":startMs, "$lte":endMs};
    }

    let dikuaiList = await dikuaiData.selectToParam({});
    let diKuaiMap = {};
    dikuaiList.forEach(info => {
        let {dId, code} = info;
        diKuaiMap[dId] = code;
    });

    let diKuaiList = await nongziData.selectToParam( param );

    let dataList = [];
    diKuaiList.forEach(item => {
        let {nzType, dIdList, plantType, count, useTime } = item;
        let didStr = "";
        dIdList.forEach(dId => {
            didStr += `${diKuaiMap[dId] || "-"}`;
        })
        dataList.push({
            nzType:changeEnumValue(NONGZITYPE, nzType),
            dIdList:didStr,
            plantType:changeEnumValue(PLANTTYPE, plantType),
            count,
            useTime:moment(useTime).format("YYYY-MM-DD"),
            useTimeNum:useTime
        });
    });

    dataList.sort( (a, b) => {
        return b.useTimeNum - a.useTimeNum;
    })

    return {dataList};
}


/**
 * 修改农资 暂无使用
 * @param reqUser 
 * @param nzId 
 * @param param 
 * @returns 
 */
export async function updateNongZi(reqUser, nzId, param) {
    let funName = `修改农资`;
    eccFormParam(funName, NongZiConfig, param);
    eccEnumValue(funName, "nzType", NONGZITYPE, param.nzType);
    eccEnumValue(funName, "plantType", PLANTTYPE, param.plantType);

    let dikuaiList = await dikuaiData.selectToParam({});
    let diKuaiMap = {};
    dikuaiList.forEach(info => {
        let {dId, code} = info;
        diKuaiMap[dId] = code;
    });
    if (!diKuaiMap[param.dIdList]) throw new BizError(ERRORENUM.地块不存在, param.dIdList);


    let nongZiInfo = await nongziData.findOne({nzId});

    
    nongZiInfo.nzType = param.nzType;//农资类型
    nongZiInfo.dIdList = param.dIdList;//地块id
    nongZiInfo.plantType = param.plantType; //种植种类【枚举】 PLANTTYPE
    nongZiInfo.count = param.count;//用量
    nongZiInfo.useTime = param.useTime; //用时间

    await nongZiInfo.save();

    return successResult();
}


/**
 * 农资回显 暂无使用
 * @param nzId 
 * @returns 
 */
export async function nongZiInfo(nzId) {
    let nongZiInfo = await nongziData.findOne({nzId});

    let dataInfo = {
        nzType:nongZiInfo.nzType,//农资类型
        // dIdList:didStr, //地块id
        dIdList:nongZiInfo.dIdList[0],//地块id
        plantType:nongZiInfo.plantType,//种植种类【枚举】 PLANTTYPE
        count:nongZiInfo.count,//用量
        useTime:nongZiInfo.useTime//时间
    };

    return {dataInfo}
}


/**
 * 农资列表 暂无使用
 * @param selectStr 
 * @param dId 
 * @param nzType 
 * @param useTime 
 * @returns 
 */
export async function nongZiListToPage(nzType:number, dId:string, useTime:number, pageNumber:number) {
    let funName = "农资列表";
    let param:any = {};
    if (nzType) {
        eccEnumValue(funName, "nzType", NONGZITYPE, nzType);
        param.nzType = nzType;
    }
    if (dId) {
        param.dIdList = {"$in":dId};
    }
    if (useTime) {
        let startMs = moment(useTime).startOf("day").valueOf();
        let endMs = moment(useTime).endOf("day").valueOf();
        param.useTime = {"$gte":startMs, "$lte":endMs};
    }

    let dikuaiList = await dikuaiData.selectToParam({});
    let diKuaiMap = {};
    dikuaiList.forEach(info => {
        let {dId, code} = info;
        diKuaiMap[dId] = code;
    });

    let nongziList = await nongziData.findDataToParamToSortPage( param, {useTime:-1}, pageNumber );
    let dataCount = await nongziData.findDataToParamCouant({});

    let dataList = [];
    nongziList.forEach(item => {
        let {nzId, nzType, dIdList, plantType, count, useTime, ct } = item;
        let didStr = "";
        let area = "";
        let purpose = "";
        let size = "";
        dIdList.forEach(dId => {
            didStr += `${diKuaiMap[dId] || "-"}`;
        })
        dataList.push({
            nzId,
            nzType:changeEnumValue(NONGZITYPE, nzType), //农资类型
            dIdList:didStr, //地块id
            plantType:changeEnumValue(PLANTTYPE, plantType), //种植种类【枚举】 PLANTTYPE
            count, //用量
            useTime:moment(useTime).format("YYYY-MM-DD"), //时间
            ct:moment(ct).format("YYYY-MM-DD"), //创建时间
            area, //所属区域
            purpose:changeEnumValue(PURPOSE, purpose), //地块用途
            size, //地块面积
        });
    });

    return {dataList, dataCount}
}


