package cn.ezhizao.project.business.product.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.product.domain.BizProduct;
import cn.ezhizao.project.business.product.service.IBizProductService;
import cn.ezhizao.project.business.technologicalProcess.domain.BizTechnologicalProcess;
import cn.ezhizao.project.business.technologicalProcess.service.IBizTechnologicalProcessService;
import cn.ezhizao.project.business.technologicalProcessDetailDrawing.domain.BizTechnologicalProcessDetailDrawing;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品管理Controller
 *
 * @author ezhizao
 * date    2023-11-13
 */
@RestController
@RequestMapping("/business/product")
public class BizProductController extends BaseController {
    @Resource
    private IBizProductService bizProductService;
    @Resource
    HttpServletRequest request;

    @Resource
    private IBizTechnologicalProcessService bizTechnologicalProcessService;
    /**
     * 查询产品管理列表
     */
    @GetMapping("/list")
    @Log(title = "产品列表查询", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizProduct bizProduct) throws NoSuchFieldException, IllegalAccessException {
//        final String tenantId = request.getHeader("tenantId");
//        bizProduct.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProduct> list = bizProductService.getList(bizProduct);
        return getDataTable(list);
    }
    @GetMapping("/getTotal")
    @Log(title = "产品列表查询总数", businessType = BusinessType.SELECT)
    public AjaxResult getTotal()
    {
        BizProduct item = new BizProduct();
        List<BizProduct> list = bizProductService.query().eq("deleted",0).list();
        int size = list.size();
        return success(size);
    }
    /**
     * 查询产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:product:list')")
    @Log(title = "查询工艺信息", businessType = BusinessType.SELECT)
    @GetMapping("/getTechnological")
    public AjaxResult list(BizTechnologicalProcess bizTechnologicalProcess) throws NoSuchFieldException, IllegalAccessException {
//        final String tenantId = request.getHeader("tenantId");
//        bizProduct.setTenantId(Long.valueOf(tenantId));
        List<BizTechnologicalProcess> list = bizTechnologicalProcessService.getTechnological(bizTechnologicalProcess);
        return success(list);
    }
    @GetMapping("/getProductsForSupplier")
    @Log(title = "外协商能配置的产品查询", businessType = BusinessType.SELECT)
    public TableDataInfo getProductsForSupplier(BizProduct bizProduct) {
//        final String tenantId = request.getHeader("tenantId");
//        bizProduct.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProduct> list = bizProductService.getProductsForSupplier(bizProduct);
        return getDataTable(list);
    }
    @GetMapping("/getProductsForOutsource")
    @Log(title = "外协商配置的产品查询", businessType = BusinessType.SELECT)
    public TableDataInfo getProductsForOutsource(BizProduct bizProduct) {
        final String tenantId = request.getHeader("tenantId");
        bizProduct.setTenantId(Long.valueOf(tenantId));
        startPage();
        List<BizProduct> list = bizProductService.getProductsForOutsource(bizProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:product:export')")
    @Log(title = "产品列表导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProduct bizProduct) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizProduct);
        List<BizProduct> list = bizProductService.getList(bizProduct);
        ExcelUtil<BizProduct> util = new ExcelUtil<>(BizProduct.class);
        util.exportExcel(response, list, "产品管理数据");
    }


    @GetMapping(value = "/getById/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(bizProductService.getById(id));
    }
    /**
     * 获取产品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:product:query')")
    @Log(title = "产品信息查看", businessType = BusinessType.OTHER)
    @GetMapping(value = "/getinfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProductService.getById(id));
    }

    /**
     * 新增产品管理
     */
    @PreAuthorize("@ss.hasPermi('business:product:add')")
    @Log(title = "产品信息新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizProduct bizProduct) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizProduct.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizProductService.save(bizProduct));
    }

    /**
     * 修改产品管理
     */
    @PreAuthorize("@ss.hasPermi('business:product:edit')")
    @Log(title = "产品信息编辑", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody BizProduct bizProduct) throws NoSuchFieldException, IllegalAccessException {
        final String tenantId = request.getHeader("tenantId");
        bizProduct.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizProductService.updateById(bizProduct));
    }

    /**
     * 删除产品管理
     */
    @PreAuthorize("@ss.hasPermi('business:product:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        List<BizTechnologicalProcess> productId = bizTechnologicalProcessService.query().in("product_id", ids).list();
        if (!productId.isEmpty()) {
            return AjaxResult.error("产品管理中存在工艺信息，不能删除");
        }
        return toAjax(bizProductService.removeBatchByIds(ids));
    }
}
