/**
 * 销售
 */
import { TABLENAME } from "../../config/dbEnum";
import { XiaoShouConfig } from "../../config/eccParam";
import { PLANTTYPE, XIAOSHOUQUXIANG } from "../../config/enum";
import * as xiaoshouData from "../../data/xiaoshou";
import * as dikuaiData from "../../data/dikuai";
import { randomId, successResult } from "../../tools/system";
import { changeEnumValue, eccEnumValue } from "../../util/verificationEnum";
import { eccFormParam } from "../../util/verificationParam";
import moment = require("moment");
import { BizError } from "../../util/bizError";
import { ERRORENUM } from "../../config/errorEnum";

/**
 * 添加销售
 * @param reqUser 
 * @param param 
 * @returns 
 */
export async function addXiaoShou(reqUser, param) {
    let funName = `添加销售`;
    eccFormParam(funName, XiaoShouConfig, param );
    eccEnumValue(funName, "quXiang", XIAOSHOUQUXIANG, param.quXiang);
    eccEnumValue(funName, "plantType", PLANTTYPE, param.plantType);
    
    let addInfo = {
        xsId:randomId(TABLENAME.销售),
        quXiang:param.quXiang,
        plantType:param.plantType,
        operationTime:param.operationTime,
        weight:param.weight,
        ct:new Date().valueOf(),
        createUser:reqUser.userId
    };

    await xiaoshouData.addData(addInfo)

    return successResult();
}


/**
 * 销售列表
 * @param quXiang 销售去向
 * @param plantType 
 * @param operationTime 
 * @returns 
 */
export async function xiaoShouList(quXiang:number, plantType:number, operationTime:number, pageNumber:number) {
    let funName = "销售列表";
    let param:any = {};
    if (plantType) {
        eccEnumValue(funName, "plantType", PLANTTYPE, plantType);
        param.plantType = plantType;
    }
    if (quXiang) {
        eccEnumValue(funName, "quXiang", XIAOSHOUQUXIANG, quXiang);
        param.quXiang = quXiang;
    }
    if (operationTime) {
        let startMs = moment(operationTime).startOf("day").valueOf();
        let endMs = moment(operationTime).endOf("day").valueOf();
        param.operationTime = {"$gte":startMs, "$lte":endMs};
    }

    let xiaoShouData = await xiaoshouData.findDataToParamToSortPage(param, {operationTime:-1}, pageNumber);
    let dataCount = await xiaoshouData.selectCountByParam(param);

    let dataList = [];
    xiaoShouData.forEach(item => {
        let {xsId, plantType, quXiang, operationTime, weight } = item;
        
        dataList.push({
            xsId,
            plantType:changeEnumValue(PLANTTYPE, plantType),
            quXiang:changeEnumValue(XIAOSHOUQUXIANG, quXiang),
            operationTime:moment(operationTime).format("YYYY-MM-DD"),
            weight,
            operationTimeNum:operationTime
        });
    });

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

    return {dataList, dataCount}
}


/**
 * 修改销售
 * @param reqUser 
 * @param nzId 
 * @param param 
 * @returns 
 */
 export async function updateXiaoShou(reqUser, xsId, param) {
    let funName = `修改销售`;
    eccFormParam(funName, XiaoShouConfig, param );
    eccEnumValue(funName, "quXiang", XIAOSHOUQUXIANG, param.quXiang);
    eccEnumValue(funName, "plantType", PLANTTYPE, param.plantType);

    let xiaoShouInfo = await xiaoshouData.selectOne({xsId});
    
    xiaoShouInfo.plantType = param.plantType;//销售种类【枚举】 PLANTTYPE
    xiaoShouInfo.quXiang = param.quXiang;//销售去向
    xiaoShouInfo.operationTime = param.operationTime;//销售时间
    xiaoShouInfo.weight = param.weight;//销售重量

    await xiaoShouInfo.save();

    return successResult();
}


/**
 * 销售回显
 * @param nzId 
 * @returns 
 */
export async function xiaoShouInfo(xsId) {
    let xiaoShouInfo = await xiaoshouData.selectOne({xsId});

    let dataInfo = {
        plantType:changeEnumValue(PLANTTYPE, xiaoShouInfo.plantType),//种植种类【枚举】 PLANTTYPE
        quXiang:changeEnumValue(XIAOSHOUQUXIANG, xiaoShouInfo.quXiang),//销售去向
        operationTime:moment(xiaoShouInfo.operationTime).format("YYYY-MM-DD"),//销售时间
        weight:xiaoShouInfo.weight//销售重量
    };

    return dataInfo;
}


/**
 * 删除销售
 * @param nzId 
 * @returns 
 */
export async function deleteXiaoShou(xsId) {
    let xiaoShouInfo = await xiaoshouData.selectOne({xsId});
    if (!xiaoShouInfo || !xiaoShouInfo.xsId) throw new BizError(ERRORENUM.数据不存在);

    await xiaoshouData.deleteData({xsId});

    return successResult();
}








