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

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

import cn.ezhizao.common.utils.uuid.SnowflakeIdWorker;
import cn.ezhizao.project.business.entrust.domain.BizEntrust;
import cn.ezhizao.project.business.entrust.service.IBizEntrustService;
import org.springframework.security.access.prepost.PreAuthorize;
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;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrder;
import cn.ezhizao.project.business.workOrder.service.IBizWorkOrderService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * 工单信息Controller
 *
 * @author ruoyi
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/business/workOrder")
public class BizWorkOrderController extends BaseController
{
    @Resource
    private IBizWorkOrderService bizWorkOrderService;

    @Resource
    private IBizEntrustService entrustService;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 查询工单信息列表
     */
    @PreAuthorize("@ss.hasAnyPermi('business:workOrder:list,business:workOrder:noContract:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);

        startPage();
        //排序
        startOrderBy();
        //根据客户名称模糊查询去除两边空格
        if( bizWorkOrder.getCompanyName()!=null){
            if(!bizWorkOrder.getCompanyName().equals(""))
                bizWorkOrder.setCompanyName(bizWorkOrder.getCompanyName().trim());
        }
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        BizEntrust entrustConditions = new BizEntrust();
        entrustConditions.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
        List<BizEntrust> entrusts = entrustService.getList(entrustConditions);
        list.forEach(v -> {
            v.setEntrusts(entrusts.stream().filter(e -> e.getWorkOrderId().equals(v.getId())).collect(Collectors.toList()));
        });
        return getDataTable(list);
    }

    /**
     * 导出工单信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:workOrder:export')")
    @Log(title = "工单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        ExcelUtil<BizWorkOrder> util = new ExcelUtil<BizWorkOrder>(BizWorkOrder.class);
        util.exportExcel(response, list, "工单信息数据");
    }

    /**
     * 获取工单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:workOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizWorkOrderService.getById(id));
    }

    /**
     * 新增工单信息
     */
    @PreAuthorize("@ss.hasPermi('business:workOrder:add')")
    @Log(title = "工单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);
        bizWorkOrder.setWorkOrderNo("workOrder_" + snowflakeIdWorker.nextId());
        return toAjax(bizWorkOrderService.save(bizWorkOrder));
    }

    /**
     * 修改工单信息
     */
    @PreAuthorize("@ss.hasPermi('business:workOrder:edit')")
    @Log(title = "工单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);
        return toAjax(bizWorkOrderService.updateById(bizWorkOrder));
    }

    /**
     * 删除工单信息
     */
    @PreAuthorize("@ss.hasPermi('business:workOrder:remove')")
    @Log(title = "工单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizWorkOrderService.removeBatchByIds(ids));
    }

    @PreAuthorize("@ss.hasAnyPermi('business:customer:service:once:transactor,business:customer:service:loop:transactor')")
    @PostMapping("setService")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult setService(@RequestBody BizWorkOrder workOrder) {
        List<BizWorkOrder> workOrders = bizWorkOrderService.query().in("id", workOrder.getIds()).list();
        workOrders.forEach(l -> l.setServiceId(workOrder.getServiceId()));
        return toAjax(bizWorkOrderService.updateBatchById(workOrders));
    }
}
