package com.bonus.sgzb.app.controller;
import cn.hutool.core.collection.CollUtil;
import com.bonus.sgzb.base.api.domain.LeaseApplyDetails;
import com.bonus.sgzb.base.api.domain.LeaseApplyInfo;
import com.bonus.sgzb.base.api.domain.TmTask;
import com.bonus.sgzb.app.service.LeaseApplyDetailsService;
import com.bonus.sgzb.app.service.LeaseApplyInfoService;
import com.bonus.sgzb.app.service.TmTaskService;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务表(tm_task)表控制层
 *
 * @author 阮世耀
 */
@RestController
@RequestMapping("/tm_task")
public class TmTaskController {

    /**
     * 服务对象
     */
    @Resource
    private TmTaskService tmTaskService;  // 任务表Service

    @Resource
    private LeaseApplyInfoService leaseApplyInfoService; // 领料申请表Service

    @Resource
    private LeaseApplyDetailsService leaseApplyDetailsService; // 领料申请明细表Service

    /**
     * 往来单位提交工程领用机具信息
     *
     * @return 结果
     */
    @Log(title = "任务表", businessType = BusinessType.INSERT)
    @PostMapping("submitLeaseApply")
    public AjaxResult submitLeaseApply(@RequestBody TmTask task) {
        if (StringUtils.isNull(task)) {
            return AjaxResult.error("参数错误");
        }
        try {
            // 创建任务
            boolean addTaskResult = tmTaskService.insertSelective(task) > 0;
            if (addTaskResult && task.getLeaseApplyInfo() != null) {
                if (CollUtil.isNotEmpty(task.getLeaseApplyDetails())) {
                    return AjaxResult.error("领料设备明细为空，请重新选择后上传!");
                }
                if (StringUtils.isNull(task.getLeaseApplyInfo())) {
                    return AjaxResult.error("领料任务信息为空，请重新选择后上传!");
                }

                // 获取任务编号
                String taskId = task.getId();
                // 根据设备所属分公司拆分集合
                List<List<LeaseApplyDetails>> deviceByCompanyList = CollUtil.group(task.getLeaseApplyDetails(), LeaseApplyDetails::getCompanyId);

                // 对拆分后的集合进行遍历
                for (List<LeaseApplyDetails> leaseApplyDetailsList : deviceByCompanyList) {
                    // 判断拆分后的集合内是否有数据
                    if (CollUtil.isNotEmpty(leaseApplyDetailsList)) {
                        // 对领料任务表的对象做数据处理
                        LeaseApplyInfo leaseApplyInfo = task.getLeaseApplyInfo();
                        leaseApplyInfo.setCode(tmTaskService.genderLeaseCode()); // 创建领料单号
                        leaseApplyInfo.setTaskId(Integer.valueOf(taskId)); // 设置任务ID
                        leaseApplyInfo.setCompanyId(leaseApplyDetailsList.get(0).getCompanyId()); // 设置设备所属分公司,用于交给哪家审核
                        leaseApplyInfo.setType("2"); // 设置审批层级，先固定2层，后期根据接口传入Type区分来源设定

                        // 创建领料任务，返回领料任务编号
                        boolean addLeaseTaskResult = leaseApplyInfoService.genderLeaseCode(leaseApplyInfo) > 0;
                        // 领料任务创建完成，进行领料任务明细插入
                        if (addLeaseTaskResult) {
                            // 领料任务编号
                            Integer leaseTaskId = leaseApplyInfo.getId();
                            if (StringUtils.isNotNull(leaseTaskId)) {
                                // 插入领料任务明细
                                boolean addLeaseTaskDetailsResult = leaseApplyDetailsService.batchInsert(leaseApplyDetailsList) > 0;
                                if (addLeaseTaskDetailsResult) {
                                    System.out.println("领料任务创建成功");
                                } else {
                                    System.out.println("领料任务创建成功，但领料任务明细插入失败");
                                }
                            }
                        } else {
                            return AjaxResult.error("创建领料任务失败,或领料明细为空");
                        }
                    }
                }
                if (StringUtils.isEmpty(taskId)) { return AjaxResult.error("任务编号为空,创建失败"); }
            } else {
                return AjaxResult.error("创建任务失败");
            }
            return AjaxResult.success("任务创建成功,已完成");
        } catch (Exception e) {
            return AjaxResult.error("创建任务失败," + e.getMessage());
        }
    }


    /**
    * 通过主键查询单条数据
    *
    * @param id 主键
    * @return 单条数据
    */
    @GetMapping("selectOne")
    public TmTask selectOne(Integer id) {
    return tmTaskService.selectByPrimaryKey(Long.valueOf(id));
    }

}
