package com.srs.stureg.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.stureg.domain.SrsStuTodo;
import com.srs.stureg.domain.VO.SrsStuSizeVo;
import com.srs.stureg.mapper.SrsStuTodoMapper;
import com.srs.stureg.service.ISrsStuTodoService;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.stureg.mapper.SrsStuSizeMapper;
import com.srs.stureg.domain.SrsStuSize;
import com.srs.stureg.service.ISrsStuSizeService;

/**
 * 学生尺码Service业务层处理
 *
 * @author srs
 * @date 2024-03-31
 */
@Service
public class SrsStuSizeServiceImpl extends ServiceImpl<SrsStuSizeMapper,SrsStuSize> implements ISrsStuSizeService {
    @Autowired
    private SrsStuSizeMapper srsStuSizeMapper;
    @Autowired
    public SrsStuTodoMapper _todoMapper;
    @Autowired
    public ISrsStuTodoService _todoService;


    @Override
    public AjaxResult submitOwnSize(SrsStuSize param){
        try{
            QueryWrapper<SrsStuSize> query = new QueryWrapper<>();
            query.eq("ksh",param.getKsh())
                    .last("limit 1");
            SrsStuSize size = srsStuSizeMapper.selectOne(query);
            if(size == null){
                size = param;
            }else{
                size.setHeight(param.getHeight());
                size.setWeight(param.getWeight());
                size.setShoes(param.getShoes());
            }

            boolean res = this.saveOrUpdate(size);
            if(!res){
                throw new Exception("保存失败");
            }

            QueryWrapper<SrsStuTodo> todoQuery = new QueryWrapper<>();
            todoQuery.eq("ksh",param.getKsh())
                    .eq("task_code","CMDJ")
                    .last("limit 1");
            SrsStuTodo todo = _todoMapper.selectOne(todoQuery);
            if(todo == null){
                todo = new SrsStuTodo();
                todo.setKsh(param.getKsh());
                todo.setStatus("1");
                todo.setTaskCode("CMDJ");
            }else{
                todo.setStatus("1");
            }
            boolean res1 = _todoService.saveOrUpdate(todo);
            if(!res1){
                throw new Exception("保存任务状态失败");
            }


            return AjaxResult.success();
        }catch(Exception ex){
            return AjaxResult.error(ex.getMessage());
        }
    }



    @Override
    public AjaxResult getStuSize(String ksh){
        QueryWrapper<SrsStuSize> query = new QueryWrapper<>();
        query.eq("ksh",ksh)
                .last("limit 1");
        SrsStuSize res = srsStuSizeMapper.selectOne(query);
        return AjaxResult.success(res);
    }


    /**
     * 查询学生尺码
     *
     * @param id 学生尺码主键
     * @return 学生尺码
     */
    @Override
    public SrsStuSize selectSrsStuSizeById(Long id) {
        return srsStuSizeMapper.selectSrsStuSizeById(id);
    }

    /**
     * 查询学生尺码列表
     *
     * @param srsStuSize 学生尺码
     * @return 学生尺码
     */
    @Override
    public List<SrsStuSize> selectSrsStuSizeList(SrsStuSize srsStuSize) {
        return srsStuSizeMapper.selectSrsStuSizeList(srsStuSize);
    }

    /**
     * 新增学生尺码
     *
     * @param srsStuSize 学生尺码
     * @return 结果
     */
    @Override
    public int insertSrsStuSize(SrsStuSize srsStuSize) {
                srsStuSize.setCreateTime(DateUtils.getNowDate());
            return srsStuSizeMapper.insertSrsStuSize(srsStuSize);
    }

    /**
     * 修改学生尺码
     *
     * @param srsStuSize 学生尺码
     * @return 结果
     */
    @Override
    public int updateSrsStuSize(SrsStuSize srsStuSize) {
                srsStuSize.setUpdateTime(DateUtils.getNowDate());
        return srsStuSizeMapper.updateSrsStuSize(srsStuSize);
    }

    /**
     * 批量删除学生尺码
     *
     * @param ids 需要删除的学生尺码主键
     * @return 结果
     */
    @Override
    public int deleteSrsStuSizeByIds(Long[] ids) {
        return srsStuSizeMapper.deleteSrsStuSizeByIds(ids);
    }

    /**
     * 删除学生尺码信息
     *
     * @param id 学生尺码主键
     * @return 结果
     */
    @Override
    public int deleteSrsStuSizeById(Long id) {
        return srsStuSizeMapper.deleteSrsStuSizeById(id);
    }

    @Override
    public List<SrsStuSizeVo> selectSrsStuSizeListVo(SrsStuSizeVo srsStuSize) {
        return srsStuSizeMapper.selectSrsStuSizeListVo(srsStuSize);
    }
}
