diff --git a/src/agricultureActivity/assets/checked.png b/src/agricultureActivity/assets/checked.png new file mode 100644 index 0000000..15dbc5d Binary files /dev/null and b/src/agricultureActivity/assets/checked.png differ diff --git a/src/agricultureActivity/assets/normal.png b/src/agricultureActivity/assets/normal.png new file mode 100644 index 0000000..e501c01 Binary files /dev/null and b/src/agricultureActivity/assets/normal.png differ diff --git a/src/agricultureActivity/index.tsx b/src/agricultureActivity/index.tsx new file mode 100644 index 0000000..1ce6292 --- /dev/null +++ b/src/agricultureActivity/index.tsx @@ -0,0 +1,361 @@ +import HeaderNation from "../components/HeaderNation"; +import StatusBar from "../components/StatusBar"; +import OuterFrame from "../components/OuterFrame"; +import { APP_FULL_HEIGHT } from "../config"; +import { View, ScrollView, Text, Picker, Image, Textarea } from "@tarojs/components"; +import { useState, useEffect } from "react"; +import NormalIcon from "./assets/normal.png"; +import CheckedIcon from "./assets/checked.png"; +import useStore from "../storage"; +import Taro, { getCurrentInstance } from "@tarojs/taro"; +import { getFarmDefineTree, getFodder, postCreatePlan, getBatchCode } from "../api/agricultureActivity"; +import { cropGrowPage } from "../api/monitor"; + +// 从Date对象中获取年月日 不足两位前面补0 +const getYearMonthDay = (timer) => { + const year = timer.getFullYear(); + const month = (timer.getMonth() + 1) > 9 ? timer.getMonth() + 1 : '0' + (timer.getMonth() + 1); + const day = timer.getDate() > 9 ? timer.getDate() : '0' + timer.getDate(); + return `${year}-${month}-${day}`; +} + +export default function AgricultureActivity() { + // 获取组件的路由对象 获取传入组件的参数 重渲染不会导致navigateTo传来的参数丢失 + const router = getCurrentInstance().router; + const plotId = router && router.params.plotId; // 在生长监控界面选择的地块ID + const plotList = useStore((store:any) => store.plotList); // 地块列表 + + // 设置当前选中的地块下标为在生长监控界面选择的地块 + let curPlotIdx: number = 0; + if (plotId) { + for (let i = 0; i < plotList.length; ++i) { + if (plotList[i].plotId === plotId) { + curPlotIdx = i; + break; + } + } + } + + // 地块 or 蟹塘 下标 + const [curPlot, setCurPlot] = useState(curPlotIdx); + + // 农事类型 or 农事阶段 + const [curType, setCurType] = useState(0); // 当前农事类型下标 + const [types, setTypes] = useState([]); // 农事类型数组 + + // 获取农事类型 根据storage中的CREATOR的值进行筛选 + const getTypes = async () => { + const res = await getFarmDefineTree({ parentId: 0, status: 1 }); + if (!res || !res.data || !Array.isArray(res.data)) return; + + const creator = Taro.getStorageSync("CREATOR"); + const tmpList = res.data.filter(ele => ele.creator === creator); + console.log("农事类型: ", tmpList); + + // 投喂饲料ID为21 + let tmp = tmpList.findIndex(ele => ele.id === 21); + setCurType(tmp === -1 ? 0 : tmp); + setTypes(tmpList); + } + useEffect(() => { + getTypes(); + console.log("地块列表: ", plotList); + }, []); + + // 操作类型 下标和数组 + const [curOperation, setCurOperation] = useState(0); + const operations = ["机械作业", "人工作业"]; + + // 开始时间 YYYY-MM-DD + const date = new Date(); + const curDate = getYearMonthDay(date); + const [startDate, setStartDate] = useState(curDate); + + // 投入品 下标和数组 + const [curFodder, setCurFodder] = useState(0); + const [fodder, setFodder] = useState([]); + const [curGrowth, setCurGrowth] = useState(); + + // 根据生长期名称获取投入品列表 + const getFodderList = async (growthPeriod?: string) => { + const res = await getFodder({ growthPeriod }); + const list = res.data; + if (!list || !Array.isArray(list)) { + return []; + } else { + return list; + } + } + + // 获取所有投入品 + const getAllFodder = async () => { + // 获取当前地块的当前生命周期 + // 目前获取当前生命周期的方式是生长期的第一个 + const { data } = await cropGrowPage({ belongPlot: plotList[curPlot].plotId }); + const { list } = data; + console.log("生长期: ", list); + if (!list || !Array.isArray(list)) return; + + const curPeriod = list[0]; + let feedList: any[] = []; + // 当前生长期有值 就查当前生长期的投入品 + if (curPeriod) { + feedList = await getFodderList(curPeriod.growth); + } + // 当前生长期没值 或者 上面查出的投入品列表为空 则查询所有生长期的投入品 + if (!curPeriod || feedList.length === 0) { + feedList = await getFodderList(); + } + + console.log("投入品: ", feedList); + setFodder(feedList); + setCurGrowth(curPeriod); + } + + // 负责人 + const [person, setPerson] = useState({ name: "", id: undefined }); + + // 批次码 + const [batchCode, setBatchCode] = useState(); + + const getBatchCodeByPlotId = async () => { + const res = await getBatchCode({ belongPlot: plotList[curPlot].plotId }); + console.log("批次码列表: ", res); + if (!res || !res.data || !Array.isArray(res.data.list) || res.data.list.length === 0) { + setBatchCode(undefined); + } else { + setBatchCode(res.data.list[0].batchCode); + } + } + + // 地块切换时 重新获取投喂农资 负责人 批次码 + useEffect(() => { + getAllFodder(); + setPerson({ name: plotList[curPlot].contact, id: undefined }); + getBatchCodeByPlotId(); + }, [curPlot]); + + // 备注 + const [remark, setRemark] = useState(""); + + // 提交表单 + const handleClickSubmit = async (e: any) => { + e.stopPropagation(); + + // 计划名称 饲料投喂-投入品名称-投入品数量(投入品单位) + let curPlanName = types[curType].defineName; + const item = fodder[curFodder]; + curPlanName = curPlanName + "-" + item.name + "-" + item.standard + "(" + item.unit + ")"; + + // 创建表单对象 开始时间 等于 结束时间 + const data = { + planName: curPlanName, + belongPark: plotList[curPlot].parkId, + parkName: curGrowth ? curGrowth.parkName : undefined, + belongPlot: plotList[curPlot].plotId, + plotName: plotList[curPlot].name, + cropId: curGrowth ? curGrowth.cropId : undefined, + cropName: curGrowth ? curGrowth.cropName : undefined, + cropType: curGrowth ? curGrowth.cropType : undefined, + planState: "0", + personId: undefined, // 目前没有联系人ID + personName: person.name, + startTime: new Date(`${startDate}T00:00:00`).getTime(), + endTime: new Date(`${startDate}T23:59:59`).getTime(), + planArea: undefined, + area: undefined, + finishArea: undefined, + farmDefineType: types[curType].id, // 饲料投喂阶段的ID + batchCode: batchCode, + planDesc: curPlanName, + remark: remark, + modeOperation: curOperation, + createTime: new Date().getTime(), + taskPublisher: Taro.getStorageSync("USER_ID") + } + + try { + console.log("表单数据: ", data); + const res = await postCreatePlan(data); + console.log("添加结果: ", res); + Taro.showToast({ + title: "添加成功", + icon:'success', + duration: 500, + }); + let timer = setInterval(() => { + Taro.navigateBack(); + clearInterval(timer); + }, 500); + } catch (e) { + console.log("提交表单出错: ", e); + } + } + + return ( + + + + + + + + *蟹塘 + { + setCurPlot(Number(event.detail.value)); + }} + > + + { plotList[curPlot].name }{">"} + + + + + + *农事类型 + { + const idx = Number(event.detail.value); + // 投喂饲料才能被设置 21是投喂饲料的id + if (21 === types[idx].id) { + setCurType(idx); + } + }} + > + + { (types.length !== 0) && types[curType].defineName } + {">"} + + + + + + 作业方式 + { + setCurOperation(Number(event.detail.value)); + }} + > + + { operations[curOperation] }{">"} + + + + + + *农事作业日期 + { + setStartDate(event.detail.value); + }} + > + + { startDate }{">"} + + + + + { + (types.length > 0 && types[curType].id === 21) && ( + + + + 投喂农资 + + + + { + fodder.length > 0 && fodder.map((item, index) => ( + <> + { + e.stopPropagation(); + setCurFodder(index); + }} + > + { item.name } + + { + index !== curFodder && ( + <> + { item.standard }{ item.unit } + + + ) + } + { + index === curFodder && ( + <> + { item.standard }{ item.unit } + + + ) + } + + + + + )) + } + + + ) + } + + + *负责人 + { person.name } + + + + 备注 + + +