package com.ruoyi.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionRecord;
import com.ruoyi.web.base.domain.param.ProductionPlanRequest;
import com.ruoyi.web.base.domain.param.ProductionRecordRequest;
import com.ruoyi.web.base.service.IProductionRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@RestController
@RequestMapping("/production-record")
public class ProductionRecordController {

    @Autowired
    private IProductionRecordService productionRecordService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产记录添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionRecord record, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        record.setOrgId(loginOrgId);
        record.setCreateBy(getUsername());
        return success(productionRecordService.save(record));
    }

    @ApiOperation("生产记录修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionRecord record, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        record.setUpdateBy(getUsername());
        return success(productionRecordService.updateById(record));
    }

    @ApiOperation("生产记录删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionRecordService.removeById(id);
        }
        return success();
    }

    @ApiOperation("生产记录列表")
    @PostMapping("/list")
    public AjaxResult listAll(  @RequestBody ProductionRecordRequest productionPlanParam, HttpServletRequest request) {
        QueryWrapper<ProductionRecord> wrapper =  getWrapper(productionPlanParam);
        return success(productionRecordService.list(
                wrapper
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产记录分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody ProductionRecordRequest productionPlanParam, HttpServletRequest request

    ) {

        QueryWrapper<ProductionRecord> wrapper =  getWrapper(productionPlanParam);
        return success(productionRecordService.page(
                new Page<ProductionRecord>(productionPlanParam.getPageNum(), productionPlanParam.getPageSize()),
                wrapper
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    private QueryWrapper<ProductionRecord> getWrapper(ProductionRecordRequest productionPlanParam) {
        QueryWrapper<ProductionRecord> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getProductionType()),"production_type",productionPlanParam.getProductionType());
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getBatch()),"batch",productionPlanParam.getBatch());
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getInspectResult()),"inspect_result",productionPlanParam.getInspectResult());
        wrapper.ge(StringUtils.isNotEmpty(productionPlanParam.getLevelstart()),"grade",productionPlanParam.getLevelstart());
        wrapper.le(StringUtils.isNotEmpty(productionPlanParam.getLevelEnd()),"grade",productionPlanParam.getLevelEnd());
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getPigBatch()),"getPig_batch",productionPlanParam.getPigBatch());
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getProcessType()),"processing_type",productionPlanParam.getProcessType());
        wrapper.le(StringUtils.isNotEmpty(productionPlanParam.getRecordDateEnd()),"production_record_date",productionPlanParam.getRecordDateEnd());
        wrapper.ge(StringUtils.isNotEmpty(productionPlanParam.getRecordDatestart()),"production_record_date",productionPlanParam.getRecordDatestart());
        wrapper.like(StringUtils.isNotEmpty(productionPlanParam.getVariety()),"variety",productionPlanParam.getVariety());
        wrapper.ge(StringUtils.isNotEmpty(productionPlanParam.getWhiteNostart()),"white_strip_number",productionPlanParam.getVariety());
        wrapper.le(StringUtils.isNotEmpty(productionPlanParam.getWhiteNoEnd()),"white_strip_number",productionPlanParam.getWhiteNoEnd());
        wrapper.le(StringUtils.isNotEmpty(productionPlanParam.getAnimalIdentification()),"animal_identification",productionPlanParam.getAnimalIdentification());
        return wrapper;

    }

    @ApiOperation("生产记录详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionRecordService.getById(id));
    }
}