<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\cost;


use app\common\model\cost\CostBudgetAdjust;
use app\common\model\cost\CostBudgetAdjustDetail;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * CostBudgetAdjust逻辑
 * Class CostBudgetAdjustLogic
 * @package app\adminapi\logic\cost
 */
class CostBudgetAdjustLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/12/18 15:17
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $costBudgetAdjust = CostBudgetAdjust::create([
                'approve_id' => $params['approve_id'],
                'years' => $params['years'],
                'document_preparation_time' => $params['document_preparation_time'],
                'remark' => $params['remark'],
                'annex' => $params['annex'],
            ]);
            foreach ($params['detail'] ?? [] as $item)
            {       
                CostBudgetAdjustDetail::create([
                    'cost_budget_adjust_id' => $costBudgetAdjust->id,
                    'cost_subject_id' => $item['cost_subject_id'] ?? 0,
                    'dept_id' => $item['dept_id'] ?? 0,
                    'month1' => $item['month1'] ?? 0,
                    'month2' => $item['month2'] ?? 0,
                    'month3' => $item['month3'] ?? 0,
                    'month4' => $item['month4'] ?? 0,
                    'month5' => $item['month5'] ?? 0,
                    'month6' => $item['month6'] ?? 0,
                    'month7' => $item['month7'] ?? 0,
                    'month8' => $item['month8'] ?? 0,
                    'month9' => $item['month9'] ?? 0,
                    'month10' => $item['month10'] ?? 0,
                    'month11' => $item['month11'] ?? 0,
                    'month12' => $item['month12'] ?? 0
                ]);
            }

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/12/18 15:17
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            CostBudgetAdjust::where('id', $params['id'])->update([
                'approve_id' => $params['approve_id'],
                'years' => $params['years'],
                'document_preparation_time' => $params['document_preparation_time'],
                'remark' => $params['remark'],
                'annex' => $params['annex'],
            ]);

            CostBudgetAdjustDetail::where('cost_budget_adjust_id', $params['id'])->update(['delete_time' => time()]);
            foreach ($params['detail'] ?? [] as $item)
            {       
                CostBudgetAdjustDetail::where('cost_budget_adjust_id', $params['id'])->update([
                    'cost_subject_id' => $item['cost_subject_id'] ?? 0,
                    'dept_id' => $item['dept_id'] ?? 0,
                    'month1' => $item['month1'] ?? 0,
                    'month2' => $item['month2'] ?? 0,
                    'month3' => $item['month3'] ?? 0,
                    'month4' => $item['month4'] ?? 0,
                    'month5' => $item['month5'] ?? 0,
                    'month6' => $item['month6'] ?? 0,
                    'month7' => $item['month7'] ?? 0,
                    'month8' => $item['month8'] ?? 0,
                    'month9' => $item['month9'] ?? 0,
                    'month10' => $item['month10'] ?? 0,
                    'month11' => $item['month11'] ?? 0,
                    'month12' => $item['month12'] ?? 0
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2023/12/18 15:17
     */
    public static function delete(array $params): bool
    {
        CostBudgetAdjustDetail::where('cost_budget_adjust_id', $params['id'])->update(['delete_time' => time()]);
        return CostBudgetAdjust::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2023/12/18 15:17
     */
    public static function detail($params): array
    {
        $costBudgetAdjust = CostBudgetAdjust::findOrEmpty($params['id']);
        $costBudgetAdjust->detail;
        foreach ($costBudgetAdjust->detail as &$item) {
            $item->subject;
        }
        $costBudgetAdjust->org;
        $costBudgetAdjust->dept;
        return $costBudgetAdjust->toArray();
    }
}