package com.zhyc.module.produce.manage_sheep.controller;

import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.common.exception.ServiceException;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.module.base.domain.BasSheepType;
import com.zhyc.module.base.domain.BasSheepVariety;
import com.zhyc.module.base.domain.DaRanch;
import com.zhyc.module.base.service.IBasSheepTypeService;
import com.zhyc.module.base.service.IBasSheepVarietyService;
import com.zhyc.module.base.service.IDaRanchService;
import com.zhyc.module.produce.manage_sheep.domain.ScAddSheep;
import com.zhyc.module.produce.manage_sheep.service.IScAddSheepService;
import com.zhyc.module.base.domain.DaSheepfold;
import com.zhyc.module.base.service.IDaSheepfoldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

@RestController
@RequestMapping("produce/manage_sheep/add_sheep")
public class ScAddSheepController extends BaseController {
    @Autowired
    private IScAddSheepService scAddSheepService;
    @Autowired
    private IDaSheepfoldService daSheepfoldMapper;
    @Autowired
    private IBasSheepVarietyService basSheepVarietyMapper;
    @Autowired
    private IBasSheepTypeService basSheepTypeService;
    @Autowired
    private IDaRanchService daRanchService;

    //新增羊只验证
    @PreAuthorize("@ss.hasPermi('produce:add_sheep:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addSheep(@RequestBody ScAddSheep scAddSheep) {
        if (scAddSheep.getEarNumber() == null || scAddSheep.getEarNumber().isEmpty()) {
            return AjaxResult.error("耳号不能为空");
        }
        if (scAddSheep.getRanchId() == null || scAddSheep.getRanchId() == 0) {
            return AjaxResult.error("牧场不能为空");
        }
        if (scAddSheep.getSheepfold() == null || scAddSheep.getSheepfold() == 0) {
            return AjaxResult.error("羊舍不能为空");
        }
        if (scAddSheep.getBornWeight() == null) {
            return AjaxResult.error("出生体重不能为空");
        }
        if (scAddSheep.getBirthday() == null) {
            return AjaxResult.error("出生日期不能为空");
        }
        if (scAddSheep.getGender() == null) {
            return AjaxResult.error("性别不能为空");
        }
        if (scAddSheep.getVarietyId() == null) {
            return AjaxResult.error("品种不能为空");
        }

        try {
            scAddSheep.setUserId(getUserId());
            scAddSheep.setDeptId(getDeptId());
            boolean success = scAddSheepService.insertScAddSheep(scAddSheep);
            if (success) {
                return success("新增成功");
            } else {
                return AjaxResult.error("新增失败");
            }
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    //导出
    @Log(title = "羊只信息", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('produce:add_sheep:export')")
    @PostMapping("/exportForm")
    public void exportForm(HttpServletResponse response, @RequestBody ScAddSheep scAddSheep) throws IOException {
        ExcelUtil<ScAddSheep> util = new ExcelUtil<>(ScAddSheep.class);
        List<ScAddSheep> list = new ArrayList<>();
        //处理牧场名称
        if (scAddSheep.getRanchId() != null) {
            // 根据牧场ID查询牧场信息
            DaRanch ranch = daRanchService.selectDaRanchById(scAddSheep.getRanchId().longValue());
            if (ranch != null) {
                scAddSheep.setRanchName(ranch.getRanch());
            }
        }
        // 处理羊舍名称
        if (scAddSheep.getSheepfold() != null) {
            DaSheepfold fold = daSheepfoldMapper.selectDaSheepfoldById(scAddSheep.getSheepfold().longValue());
            if (fold != null) {
                scAddSheep.setSheepfoldNameExcel(fold.getSheepfoldName());
            }
        }

        //处理羊只品种名称
        if (scAddSheep.getVarietyId() != null) {
            BasSheepVariety variety = basSheepVarietyMapper.selectBasSheepVarietyById(scAddSheep.getVarietyId().longValue());
            if (variety != null) {
                scAddSheep.setVarietyName(variety.getVariety());
            }
        }

        //处理羊只类型名称
        if (scAddSheep.getTypeId() != null) {
            BasSheepType sheepType = basSheepTypeService.selectBasSheepTypeById(scAddSheep.getTypeId().intValue());
            if (sheepType != null) {
                scAddSheep.setTypeName(sheepType.getName());
            }
        }
        list.add(scAddSheep);
        util.exportExcel(response, list, "羊只信息");
    }

    //导入
    @PostMapping("/importData")
    @PreAuthorize("@ss.hasPermi('produce:add_sheep:import')")
    @Log(title = "羊只信息", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ScAddSheep> util = new ExcelUtil<>(ScAddSheep.class);
        List<ScAddSheep> list = util.importExcel(file.getInputStream());
        Long userId = getUserId();
        Long deptId = getDeptId();
        String message = scAddSheepService.importSheep(list, updateSupport, getUsername(), userId, deptId);
        return success(message);
    }

    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('produce:add_sheep:import')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ScAddSheep> util = new ExcelUtil<>(ScAddSheep.class);
        util.importTemplateExcel(response, "羊只信息模板");
    }
}
