package com.zzsmart.qomo.kn.cost.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzsmart.qomo.dao.entity.FlowDefinition;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostCount;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostModel;
import com.zzsmart.qomo.kn.cost.manage.enums.CostModelStatusEnum;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostCountMapper;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostModelMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostModelService;
import com.zzsmart.qomo.kn.cost.manage.vo.CostModelDefinitionVO;
import com.zzsmart.qomo.service.IDataFlowDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Description: app_scene_cost_model
 * @Author: jeecg-boot
 * @Date: 2024-07-29
 * @Version: V1.0
 */
@Api(tags = "cost-标准成本-模型管理")
@RestController
@RequestMapping("//cost-model")
@Slf4j
public class AppSceneCostModelController {
    @Autowired
    private IAppSceneCostModelService appSceneCostModelService;
    @Autowired
    private IDataFlowDefinitionService flowDefinitionService;
    @Autowired
    private AppSceneCostCountMapper appSceneCostCountMapper;
    @Autowired
    private AppSceneCostModelMapper costModelMapper;
    /**
     * 标准成本模型分页列表查询
     *
     * @param flowDefinition
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_scene_cost_model-分页列表查询")
    @ApiOperation(value = "标准成本模型分页列表查询", notes = "标准成本模型分页列表查询")
    @GetMapping(value = "/pageList")
    public Result<IPage<FlowDefinition>> queryPageList(FlowDefinition flowDefinition, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<FlowDefinition> queryWrapper = QueryGenerator.initQueryWrapper(flowDefinition, req.getParameterMap());
        Page<FlowDefinition> page = new Page((long) pageNo, (long) pageSize);
        LambdaQueryWrapper<FlowDefinition> wrapper = queryWrapper.lambda();
        wrapper.orderByDesc(FlowDefinition::getCreateTime);
        IPage<FlowDefinition> pageList = flowDefinitionService.queryPageList(page, wrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "标准成本模型列表", notes = "标准成本模型列表")
    @GetMapping(value = "/list")
    public Result<List<FlowDefinition>> queryList(FlowDefinition flowDefinition, HttpServletRequest req) {
        QueryWrapper<FlowDefinition> queryWrapper = QueryGenerator.initQueryWrapper(flowDefinition, req.getParameterMap());
        LambdaQueryWrapper<FlowDefinition> wrapper = queryWrapper.lambda();
        wrapper.orderByDesc(FlowDefinition::getCreateTime);
        List<FlowDefinition> list = appSceneCostModelService.queryListResult(wrapper);
        return Result.OK(list);
    }

    /**
     * 添加标准成本模型
     *
     * @param modelDefinitionVO
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-添加")
    @ApiOperation(value = "app_scene_cost_model-添加", notes = "app_scene_cost_model-添加")
    @PostMapping(value = "/add")
    public Result<FlowDefinition> add(@RequestBody CostModelDefinitionVO modelDefinitionVO) {
        try {
            FlowDefinition flowDefinition = new FlowDefinition();
            //TODO 新增平台层模型定义记录
            String flowCode = "FLOW_" + System.currentTimeMillis() + new Random(10000).nextInt();
            flowDefinition.setUpdateTime(new Date());
            flowDefinition.setCreateTime(new Date());
            flowDefinition.setCode(flowCode);
            flowDefinition.setProjectId(modelDefinitionVO.getProjectId());
            flowDefinition.setUserId("1744546658109349889");
            flowDefinition.setUserName("系统管理员");
            flowDefinition.setTenantId(-1);
            flowDefinition.setEnvironmentId(1);
            flowDefinition.setName(modelDefinitionVO.getModelName());
            FlowDefinition definition = flowDefinitionService.add(flowDefinition);
            AppSceneCostModel forecastModel = new AppSceneCostModel();
            forecastModel.setModelType(modelDefinitionVO.getModelType());
            forecastModel.setModelName(modelDefinitionVO.getModelName());
            forecastModel.setStatus(CostModelStatusEnum.Draft.getCode());
            //2.新模型记录
            forecastModel.setFlowDefinitionId(definition.getId());
            forecastModel.setUpdateTime(new Date());
            forecastModel.setCreateTime(new Date());
            costModelMapper.insert(forecastModel);

        } catch (Exception e) {
            return Result.error("新增失败");
        }
        return Result.OK("新增成功");
    }

    /**
     * 编辑
     *
     * @param flowDefinition
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-编辑")
    @ApiOperation(value = "app_scene_cost_model-编辑", notes = "app_scene_cost_model-编辑")
    @PostMapping(value = "/edit")
    public Result<FlowDefinition> edit(@RequestBody FlowDefinition flowDefinition) {
        flowDefinition.setUpdateTime(new Date());
        return Result.OK(flowDefinitionService.edit(flowDefinition));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_scene_cost_model-通过id删除")
    @ApiOperation(value = "app_scene_cost_model-通过id删除", notes = "app_scene_cost_model-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) Integer id) {
        //检查模型是否被引用或挂载
        if (id != null) {
            FlowDefinition flowDefinition = flowDefinitionService.getById(id);
            if (flowDefinition != null) {
                Integer id1 = flowDefinition.getId();
                QueryWrapper<AppSceneCostCount> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("flow_definition_id", id1);
                Long aLong = appSceneCostCountMapper.selectCount(queryWrapper);
                if (aLong > 0) {
                    return Result.error("该模型正在被引用，无法删除！");
                }
            }
        }
        flowDefinitionService.delete(id);
        return Result.OK("删除成功!");
    }

}
