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.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionSlaughter;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IProductionSlaughterService;
import com.ruoyi.web.v1.DTO.PurchaserDimension;
import com.ruoyi.web.v1.DTO.SupplierDimension;
import com.ruoyi.web.v1.model.DistributeBatch;
import com.ruoyi.web.v1.model.EntranceBatch;
import com.ruoyi.web.v1.model.Purchaser;
import com.ruoyi.web.v1.model.Supplier;
import com.ruoyi.web.v1.service.IDistributeBatchService;
import com.ruoyi.web.v1.service.IEntranceBatchService;
import com.ruoyi.web.v1.service.IPurchaserService;
import com.ruoyi.web.v1.service.ISupplierService;
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.Date;
import java.util.Map;

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

@RestController
@RequestMapping("/production-slaughter")
public class ProductionSlaughterController {

    @Autowired
    private IProductionSlaughterService productionSlaughterService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IEntranceBatchService entranceBatchService;

    @Autowired
    private IDistributeBatchService distributeBatchService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IPurchaserService purchaserService;

    @ApiOperation("生产屠宰添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionSlaughter slaughter, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        slaughter.setOrgId(loginOrgId);
        slaughter.setCreateBy(getUsername());
        slaughter.setCreateTime(new Date());
        Supplier supplierDimension = supplierService.getSupplierDimension(new SupplierDimension(slaughter.getSupplierNumber()));
        Purchaser purchaserDimension = purchaserService.getPurchaserDimension(new PurchaserDimension(slaughter.getSlaughterAgencyNumber()));
        //新增入场
        EntranceBatch entranceBatch = new EntranceBatch();
        entranceBatch.setSupplierId(supplierDimension.getId());
        entranceBatch.setEntranceTime(slaughter.getAdditionTime());
        entranceBatch.setAmount(slaughter.getPlannedSlaughterQuantity());
        entranceBatch.setCreateBy(getUsername());
        entranceBatch.setCreateTime(new Date());
        entranceBatch.setCargoOwner(slaughter.getSupplierName());
        entranceBatch.setCargoOwnerPhone(supplierDimension.getPhone());
        entranceBatch.setAnimalType("1");//默认猪
        Long entranceId = entranceBatchService.insertEntranceBatch(entranceBatch);
        if (entranceId <=0) {
            throw new RuntimeException("添加入场失败");
        }
        //新增分销
        DistributeBatch distributeBatch = new DistributeBatch();
        distributeBatch.setEntranceBatchId(entranceId);
        distributeBatch.setAmount(slaughter.getPlannedSlaughterQuantity());
        distributeBatch.setCreateBy(getUsername());
        distributeBatch.setCreateTime(new Date());
        distributeBatch.setPurchaserId(purchaserDimension.getId());
        distributeBatch.setSupplierId(supplierDimension.getId());
        int distributeBatchId = distributeBatchService.insertDistributeBatch(distributeBatch);
        if (distributeBatchId <=0) {
            throw new RuntimeException("添加分销失败");
        }
        return success(productionSlaughterService.save(slaughter));
    }

    @ApiOperation("生产屠宰修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionSlaughter slaughter, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        slaughter.setUpdateBy(getUsername());
        slaughter.setUpdateTime(new Date());
        return success(productionSlaughterService.updateById(slaughter));
    }

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

    @ApiOperation("生产屠宰列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionSlaughterService.list(
                new QueryWrapper<ProductionSlaughter>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产屠宰分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody WiteStripRequest planRequest,
            HttpServletRequest request
    ) {
        return success(productionSlaughterService.page(
                new Page<ProductionSlaughter>(planRequest.getPageNum(), planRequest.getPageSize()),
                new QueryWrapper<ProductionSlaughter>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

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