294 lines
11 KiB
C#
294 lines
11 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using SolarPower.Models;
|
|
using SolarPower.Models.Role;
|
|
using SolarPower.Repository.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SolarPower.Controllers
|
|
{
|
|
public class OperationController : MyBaseController<OperationController>
|
|
{
|
|
private readonly IUserRepository userRepository;
|
|
private readonly IOperationRepository operationRepository;
|
|
private readonly IRoleRepository roleRepository;
|
|
public OperationController(
|
|
IUserRepository userRepository,
|
|
IOperationRepository operationRepository) : base()
|
|
{
|
|
this.userRepository = userRepository;
|
|
this.operationRepository = operationRepository;
|
|
}
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
/// <summary>
|
|
/// 取得電站Option
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
public async Task<ActionResult> GetPowerStationSelectOption(int post)
|
|
{
|
|
ApiResult<List<PowerStationIdList>> apiResult = new ApiResult<List<PowerStationIdList>>();
|
|
try
|
|
{
|
|
var PowerStationIdLists = new List<PowerStationIdList>();
|
|
PowerStationIdLists = await operationRepository.GetPowerStationIdList(myUser.Id) ;
|
|
apiResult.Code = "0000";
|
|
apiResult.Data = PowerStationIdLists;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
|
|
|
var result = Json(new
|
|
{
|
|
data = apiResult
|
|
});
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 儲存計畫
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult<string>> SaveOperationPlan(OperationCreatePlanModal post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
try
|
|
{
|
|
if(post.Id == 0)
|
|
{
|
|
var now = DateTime.Now.ToString("yyyy-MM-dd");
|
|
var finalid = await operationRepository.GetCurrentSerialNumber("operation_plan_create", $"PowerStationId = {post.PowerStationId} AND CreatedAt LIKE '%{now}%'");
|
|
var newSerialNumber = GetLastSerialNumber(finalid);
|
|
var OperationPlan = new OperationCreatePlan()
|
|
{
|
|
EmailType = post.EmailType,
|
|
ScheduleNum = post.ScheduleNum,
|
|
Description = post.Description,
|
|
WorkDay = post.WorkDay,
|
|
ScheduleType = post.ScheduleType,
|
|
SerialNumber = newSerialNumber,
|
|
StartTime = post.StartTime,
|
|
PowerStationId = post.PowerStationId,
|
|
Type = post.Type,
|
|
PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber,
|
|
CreatedBy = myUser.Id
|
|
};
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"EmailType",
|
|
"ScheduleNum",
|
|
"Description",
|
|
"WorkDay",
|
|
"ScheduleType",
|
|
"SerialNumber",
|
|
"StartTime",
|
|
"PowerStationId",
|
|
"Type",
|
|
"PlanId",
|
|
"CreatedBy"
|
|
};
|
|
await operationRepository.AddOperationPlan(OperationPlan, properties);
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "新增成功";
|
|
}
|
|
else
|
|
{
|
|
var OperationPlan = new OperationCreatePlan()
|
|
{
|
|
Id = post.Id,
|
|
EmailType = post.EmailType,
|
|
ScheduleNum = post.ScheduleNum,
|
|
Description = post.Description,
|
|
WorkDay = post.WorkDay,
|
|
ScheduleType = post.ScheduleType,
|
|
StartTime = post.StartTime,
|
|
PowerStationId = post.PowerStationId,
|
|
Type = post.Type,
|
|
UpdateBy = myUser.Id
|
|
};
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"Id",
|
|
"EmailType",
|
|
"ScheduleNum",
|
|
"Description",
|
|
"WorkDay",
|
|
"ScheduleType",
|
|
"StartTime",
|
|
"PowerStationId",
|
|
"Type",
|
|
"UpdateBy"
|
|
};
|
|
await operationRepository.UpdateOperationPlan(OperationPlan, properties);
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "編輯成功";
|
|
}
|
|
|
|
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
|
return apiResult;
|
|
}
|
|
/// <summary>
|
|
/// 定時計畫datatable
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<ActionResult> OperationPlanTable(List<int> id)
|
|
{
|
|
List<OperationPlanTable> OperationPlanTable = new List<OperationPlanTable>();
|
|
ApiResult<List<OperationPlanTable>> apiResult = new ApiResult<List<OperationPlanTable>>();
|
|
try
|
|
{
|
|
apiResult.Code = "0000";
|
|
OperationPlanTable = await operationRepository.OperationPlanTable(id);
|
|
foreach (OperationPlanTable a in OperationPlanTable)
|
|
{
|
|
if(a.Type == 0)
|
|
{
|
|
a.TypeName = "清洗";
|
|
}
|
|
else
|
|
{
|
|
a.TypeName = "巡檢";
|
|
}
|
|
|
|
if(a.ScheduleType == 0)
|
|
{
|
|
a.Schedule = "每" + a.ScheduleNum.ToString() + "天" ;
|
|
}
|
|
else if (a.ScheduleType == 1)
|
|
{
|
|
a.Schedule = "每" + a.ScheduleNum.ToString() + "周";
|
|
}
|
|
else if (a.ScheduleType == 2)
|
|
{
|
|
a.Schedule = "每" + a.ScheduleNum.ToString() + "月";
|
|
}
|
|
else if (a.ScheduleType == 3)
|
|
{
|
|
a.Schedule = "每" + a.ScheduleNum.ToString() + "季";
|
|
}
|
|
else if (a.ScheduleType == 4)
|
|
{
|
|
a.Schedule = "每" + a.ScheduleNum.ToString() + "年";
|
|
}
|
|
|
|
a.StartTimeString = a.StartTime;
|
|
var crst = a.CreatedAt.Split(" ");
|
|
a.CreateTimeString = crst[0];
|
|
|
|
if(a.EmailType == 0)
|
|
{
|
|
a.EmailTypeName = "當天";
|
|
}
|
|
else if(a.EmailType == 1)
|
|
{
|
|
a.EmailTypeName = "前一天";
|
|
}
|
|
else if (a.EmailType == 2)
|
|
{
|
|
a.EmailTypeName = "前兩天";
|
|
}
|
|
else if (a.EmailType == 3)
|
|
{
|
|
a.EmailTypeName = "前三天";
|
|
}
|
|
|
|
a.Function = @"
|
|
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
|
|
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
|
|
}
|
|
|
|
apiResult.Data = OperationPlanTable;
|
|
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = exception.ToString();
|
|
}
|
|
var result = Json(new
|
|
{
|
|
data = apiResult
|
|
});
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 刪除定時計畫
|
|
/// </summary>
|
|
/// <param name="Id"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult<string>> DeleteOneOperationPlan(int Id)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
OperationCreatePlan operation = new OperationCreatePlan();
|
|
try
|
|
{
|
|
operation = await operationRepository.GetOneOperation(Id);
|
|
|
|
if (operation == null)
|
|
{
|
|
apiResult.Code = "9998";
|
|
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
|
return apiResult;
|
|
}
|
|
|
|
await operationRepository.DeleteOneOtherTable(Id, "operation_plan_create");
|
|
|
|
apiResult.Code = "0000";
|
|
apiResult.Msg = "刪除成功";
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + Id);
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
|
|
return apiResult;
|
|
}
|
|
public async Task<ApiResult<OperationCreatePlan>> GetOneOperationPlan(int Id)
|
|
{
|
|
ApiResult<OperationCreatePlan> apiResult = new ApiResult<OperationCreatePlan>();
|
|
OperationCreatePlan operationCreatePlan = new OperationCreatePlan();
|
|
try
|
|
{
|
|
apiResult.Code = "0000";
|
|
operationCreatePlan = await operationRepository.GetOneOperation(Id);
|
|
apiResult.Data = operationCreatePlan;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
apiResult.Code = "9999";
|
|
apiResult.Msg = exception.ToString();
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + Id);
|
|
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
|
}
|
|
return apiResult;
|
|
}
|
|
}
|
|
}
|