package com.op.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.utils.uuid.IdUtils;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.core.web.controller.BaseController;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;
import com.op.mes.domain.ProRoute;
import com.op.mes.service.IProRouteProcessService;
import com.op.mes.service.IProRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 工艺路线Controller
 *
 * @author yinjinlu
 * @date 2022-05-12
 */
@RestController
@RequestMapping("/pro/proroute")
public class ProRouteController extends BaseController {
    @Autowired
    private IProRouteService proRouteService;

    @Autowired
    private IProRouteProcessService proRouteProcessService;


    /**
     * 查询工艺路线列表
     */
    @RequiresPermissions("mes:pro:process:add")
    @GetMapping("/list")
    public TableDataInfo list(ProRoute proRoute) {
        startPage();
        List<ProRoute> list = proRouteService.selectProRouteList(proRoute);
        return getDataTable(list);
    }

    /**
     * 导出工艺路线列表
     */
    @RequiresPermissions("mes:pro:proroute:export")
    @Log(title = "工艺路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProRoute proRoute) {
        List<ProRoute> list = proRouteService.selectProRouteList(proRoute);
        ExcelUtil<ProRoute> util = new ExcelUtil<ProRoute>(ProRoute.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    /**
     * 获取工艺路线详细信息
     */
    @RequiresPermissions("mes:pro:proroute:query")
    @GetMapping(value = "/{routeId}")
    public AjaxResult getInfo(@PathVariable("routeId") String routeId) {
        return AjaxResult.success(proRouteService.selectProRouteByRouteId(routeId));
    }

    /**
     * 新增工艺路线
     */
    @RequiresPermissions("mes:pro:proroute:add")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProRoute proRoute) {
        proRoute.setRouteId(IdUtils.fastSimpleUUID());
        if (!proRouteService.checkRouteCodeUnique(proRoute)) {
            return AjaxResult.error("工艺路线编号已存在！");
        }
        return toAjax(proRouteService.insertProRoute(proRoute));
    }

    /**
     * 批量新增附件
     */
    @RequiresPermissions("mes:pro:proroute:import")
    @Log(title = "工艺路线", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importFile(@RequestBody ProRoute proRoute)
    {
        return toAjax(proRouteService.batchInsertProRoute(proRoute));
    }

    /**
     * 修改工艺路线
     */
    @RequiresPermissions("mes:pro:proroute:edit")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProRoute proRoute) {
        if (!proRouteService.checkRouteCodeUnique(proRoute)) {
            return AjaxResult.error("工艺路线编号已存在！");
        }
        return toAjax(proRouteService.updateProRoute(proRoute));
    }

    /**
     * 删除工艺路线
     */
    @RequiresPermissions("mes:pro:proroute:remove")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    //@Transactional
    @DeleteMapping("/{routeIds}")
    public AjaxResult remove(@PathVariable String[] routeIds) {
        for (String routeId : routeIds) {
            proRouteProcessService.deleteByRouteId(routeId);
            //proRouteProductService.deleteByRouteId(routeId);
            //proRouteProductBomService.deleteByRouteId(routeId);
        }
        return toAjax(proRouteService.deleteProRouteByRouteIds(routeIds));
    }
}
