package com.ruoyi.web.v2.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.app.mapper.PigpenMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsDivideCircle;
import com.ruoyi.web.v2.v1.model.JsRestInspection;
import com.ruoyi.web.v2.v1.mapper.JsDivideCircleMapper;
import com.ruoyi.web.v2.v1.mapper.JsRestInspectionMapper;
import com.ruoyi.web.v2.v1.service.IJsRestInspectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.v2.v1.utils.UploadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v2.0静养巡查登记 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-22
 */
@Service
public class JsRestInspectionServiceImpl extends ServiceImpl<JsRestInspectionMapper, JsRestInspection> implements IJsRestInspectionService {

    @Autowired
    private JsRestInspectionMapper restInspectionMapper;
    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private JsDivideCircleMapper divideCircleMapper;
    @Autowired
    private PigpenMapper pigpenMapper;

    @Transactional
    @Override
    public AjaxResult add(JsRestInspection jsRestInspection) {
        JsDivideCircle jsDivideCircle = divideCircleMapper.selectById(jsRestInspection.getDivideCircleId());
        if(jsDivideCircle != null) {
            jsRestInspection.setSeriesNo(jsDivideCircle.getSeriesNo() != null ? jsDivideCircle.getSeriesNo() : "");
        }
        restInspectionMapper.insert(jsRestInspection);
        //JsRestInspection restInspection = restInspectionMapper.selectOne(new QueryWrapper<JsRestInspection>().orderByDesc("id").last(" limit 1"));
        JsRestInspection restInspection;
        if(jsRestInspection.getId() != null && jsRestInspection.getId() > 0) {
            restInspection = restInspectionMapper.selectById(jsRestInspection.getId());
        }else {
            restInspection = restInspectionMapper.selectOne(new QueryWrapper<JsRestInspection>().orderByDesc("id").last(" limit 1"));
        }

        if ("".equals(jsDivideCircle.getRestInspectionId()) || null == jsDivideCircle.getRestInspectionId()) {
            jsDivideCircle.setRestInspectionId(restInspection.getId().toString());
        } else {
            jsDivideCircle.setRestInspectionId(jsDivideCircle.getRestInspectionId() + "," + restInspection.getId());
        }
        divideCircleMapper.updateById(jsDivideCircle);
        return success();

    }

    @Override
    public AjaxResult edit(JsRestInspection jsRestInspection) {
        restInspectionMapper.updateById(jsRestInspection);
        return success();
    }

    /* @Override
        public AjaxResult add(String restInspectionTime, String pigpenName, MultipartFile files, String restInspectionContent, String handle, Integer dealAmount, String userName, Integer divideCircleId) throws IOException {
            String s = "";
            JsRestInspection restInspection = new JsRestInspection();
            if (null!=files) {
                s = uploadImage.getImageCom(files);
                if ("上传失败".equals(s)) {
                    return error("图片上传失败，请重新上传");
                } else {
                    restInspection.setRestInspectionImg(s);
                }
            }
            restInspection.setRestInspectionTime(restInspectionTime);
            restInspection.setPigpenName(pigpenName);
            restInspection.setRestInspectionContent(restInspectionContent);
            restInspection.setHandle(handle);
            restInspection.setDealAmount(dealAmount);
            restInspection.setUserName(userName);
            restInspection.setDivideCircleId(divideCircleId);
            restInspectionMapper.insert(restInspection);
            JsDivideCircle jsDivideCircle = divideCircleMapper.selectById(divideCircleId);
            if ("".equals(jsDivideCircle.getRestInspectionId()) || null == jsDivideCircle.getRestInspectionId()) {
                jsDivideCircle.setRestInspectionId(divideCircleId.toString());
            } else {
                jsDivideCircle.setRestInspectionId(jsDivideCircle.getRestInspectionId() + "," + divideCircleId);
            }
            divideCircleMapper.updateById(jsDivideCircle);
            return success();
        }

        @Override
        public AjaxResult edit(Integer id, String restInspectionTime, MultipartFile files, String restInspectionContent, String handle, Integer dealAmount, String userName) throws IOException {
            JsRestInspection restInspection = restInspectionMapper.selectById(id);
            String s = "";
            if (null!=files) {
                s = uploadImage.getImageCom(files);
                if ("上传失败".equals(s)) {
                    return error("图片上传失败，请重新上传");
                } else {
                    restInspection.setRestInspectionImg(s);
                }
            }
            restInspection.setRestInspectionContent(restInspectionContent);
            restInspection.setHandle(handle);
            restInspection.setDealAmount(dealAmount);
            restInspection.setUserName(userName);
            restInspectionMapper.updateById(restInspection);
            return success();
        }
    */
    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        String s1 = "";
        for (String s : split) {
            restInspectionMapper.deleteById(s);

        }
        return success();
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String startTime, String endTime, String pigpenName) {
        Page<JsRestInspection> page = new Page<>(pageNum, pageSize);
        QueryWrapper<JsRestInspection> queryWrapper = new QueryWrapper<>();
        if ("".equals(startTime) || null == startTime) {

        } else {
            queryWrapper.between("rest_inspection_time", startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.orderByDesc("rest_inspection_time");
        queryWrapper.like(StringUtils.isNotEmpty(pigpenName),"pigpen_name", pigpenName);
        return success(restInspectionMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(restInspectionMapper.selectList(null));
    }

    @Override
    public AjaxResult listByPigpen(Integer pigpenId) {
        QueryWrapper<JsDivideCircle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pigpen_id", pigpenId).orderByDesc("divide_time").last(" limit 1");
        JsDivideCircle jsDivideCircle = divideCircleMapper.selectOne(queryWrapper);
        return success(jsDivideCircle);
    }

}
