using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using ${options.ModelsNamespace}.Dto;
using ${options.ModelsNamespace}.Models;
using ${options.IServicsNamespace}.${options.SubNamespace}.I${options.SubNamespace}Service;
using ${options.ApiControllerNamespace}.Extensions;
using ${options.ApiControllerNamespace}.Filters;
using ${options.BaseNamespace}Common;

namespace ${options.ApiControllerNamespace}.Controllers
{
    /// <summary>
    /// ${genTable.functionName}Controller
    /// 
    /// @tableName ${genTable.TableName}
    /// @author ${replaceDto.Author}
    /// @date ${replaceDto.AddTime}
    /// </summary>
    [Verify]
    [Route("${genTable.ModuleName}/${genTable.BusinessName}")]
    public class ${replaceDto.ModelTypeName}Controller : BaseController
    {
        /// <summary>
        /// ${genTable.FunctionName}接口
        /// </summary>
        private readonly I${replaceDto.ModelTypeName}Service _${replaceDto.ModelTypeName}Service;

        public ${replaceDto.ModelTypeName}Controller(I${replaceDto.ModelTypeName}Service ${replaceDto.ModelTypeName}Service)
        {
            _${replaceDto.ModelTypeName}Service = ${replaceDto.ModelTypeName}Service;
        }

        /// <summary>
        /// 查询${genTable.FunctionName}列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:list")]
        public IActionResult Query${replaceDto.ModelTypeName}([FromQuery] ${replaceDto.ModelTypeName}QueryDto parm)
        {
            var response = _${replaceDto.ModelTypeName}Service.GetList(parm);
            return SUCCESS(response);
        }

$if(genTable.TplCategory == "tree")
        /// <summary>
        /// 查询${genTable.FunctionName}列表树
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("treeList")]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:list")]
        public IActionResult QueryTree${replaceDto.ModelTypeName}([FromQuery] ${replaceDto.ModelTypeName}QueryDto parm)
        {
            var response = _${replaceDto.ModelTypeName}Service.GetTreeList(parm);
            return SUCCESS(response);
        }
$end

        /// <summary>
        /// 查询${genTable.FunctionName}详情
        /// </summary>
        /// <param name="${replaceDto.PKName}"></param>
        /// <returns></returns>
        [HttpGet("{${replaceDto.PKName}}")]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:query")]
        public IActionResult Get${replaceDto.ModelTypeName}(${replaceDto.PKType} ${replaceDto.PKName})
        {
            var response = _${replaceDto.ModelTypeName}Service.GetFirst(x => x.${replaceDto.PKName} == ${replaceDto.PKName});
            
            return SUCCESS(response);
        }

$if(replaceDto.ShowBtnAdd)
        /// <summary>
        /// 添加${genTable.FunctionName}
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:add")]
        [Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.INSERT)]
        public IActionResult Add${replaceDto.ModelTypeName}([FromBody] ${replaceDto.ModelTypeName}Dto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求参数错误");
            }
            var modal = parm.Adapt<${replaceDto.ModelTypeName}>().ToCreate(HttpContext);

            var response = _${replaceDto.ModelTypeName}Service.Add${replaceDto.ModelTypeName}(modal);

            return ToResponse(response);
        }
$end

$if(replaceDto.ShowBtnEdit)
        /// <summary>
        /// 更新${genTable.FunctionName}
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:edit")]
        [Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.UPDATE)]
        public IActionResult Update${replaceDto.ModelTypeName}([FromBody] ${replaceDto.ModelTypeName}Dto parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
            var modal = parm.Adapt<${replaceDto.ModelTypeName}>().ToUpdate(HttpContext);

            var response = _${replaceDto.ModelTypeName}Service.Update${replaceDto.ModelTypeName}(modal);

            return ToResponse(response);
        }
$end

$if(replaceDto.ShowBtnDelete)
        /// <summary>
        /// 删除${genTable.FunctionName}
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:delete")]
        [Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.DELETE)]
        public IActionResult Delete${replaceDto.ModelTypeName}(string ids)
        {
            int[] idsArr = Tools.SpitIntArrary(ids);
            if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }

            var response = _${replaceDto.ModelTypeName}Service.Delete(idsArr);

            return ToResponse(response);
        }
$end

$if(replaceDto.ShowBtnExport)
        /// <summary>
        /// 导出${genTable.FunctionName}
        /// </summary>
        /// <returns></returns>
        [Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:export")]
        public IActionResult Export([FromQuery] ${replaceDto.ModelTypeName}QueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _${replaceDto.ModelTypeName}Service.GetList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
            return ExportExcel(result.Item2, result.Item1);
        }
$end

$if(replaceDto.ShowBtnTruncate)
        /// <summary>
        /// 清空${genTable.FunctionName}
        /// </summary>
        /// <returns></returns>
        [Log(Title = "${genTable.FunctionName}", BusinessType = BusinessType.CLEAN)]
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:delete")]
        [HttpDelete("clean")]
        public ApiResult Clear()
        {
            if (!HttpContextExtension.IsAdmin(HttpContext))
            {
                return ApiResult.Error("操作失败");
            }
            _${replaceDto.ModelTypeName}Service.Truncate${replaceDto.ModelTypeName}();

            return ToJson(1);
        }
$end

$if(showCustomInput)
        /// <summary>
        /// 保存排序
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="value">排序值</param>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:edit")]
        [HttpGet("ChangeSort")]
        [Log(Title = "保存排序", BusinessType = BusinessType.UPDATE)]
        public IActionResult ChangeSort(int id = 0, int value = 0)
        {
            if (id <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
            var response = _${replaceDto.ModelTypeName}Service.Update(w => w.${replaceDto.PKName} == id, it => new ${replaceDto.ModelTypeName}()
            {
                //Update 字段映射
$foreach(item in genTable.Columns)
$if((item.htmlType == "customInput"))
                $item.CsharpField = value,
$end
${end}
            });
            
            return ToResponse(response);
        }
$end
    }
}