package com.mvc.service.dic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.DicChkplatMapper;
import com.mvc.entity.DicChkplat;
import com.mvc.entity.DicChkplatExample;
import com.mvc.model.DicChkplatCellModel;
import com.mvc.model.DicChkplatModel;
import com.mvc.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DicChkplatService {

    @Autowired
    private DicChkplatMapper dicChkplatMapper;

    @Autowired
    private DicChkplatCellService dicChkplatCellService;

    public Map<String, Object> findAllDicChkplat(Integer page, Integer rows) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<DicChkplat> dicChkplats = dicChkplatMapper.selectByExample(null);
        List<DicChkplatModel> resList = new ArrayList<>();
        for (DicChkplat dicChkplat : dicChkplats) {
            DicChkplatModel model = new DicChkplatModel();
            BeanUtils.copyProperties(dicChkplat, model);
            model.setCellList(dicChkplatCellService.findAllDicChkplatCell(model.getPlatId()));
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public DicChkplatModel saveDicChkplat(DicChkplatModel model) {
        model.setPlatId(StringUtil.getUuid());
        DicChkplat dicChkplat = new DicChkplat();
        BeanUtils.copyProperties(model, dicChkplat);
        int insert = dicChkplatMapper.insert(dicChkplat);
        model.setId(dicChkplat.getId());
        //保存工装集合
        List<DicChkplatCellModel> cellList = new ArrayList<>();
        Integer cellNum = model.getCellNum();
        if (cellNum != null) {
            for (int i = 1; i <= cellNum.intValue(); i++) {
                DicChkplatCellModel cellModel=dicChkplatCellService.addCellModel(model,i + "");
                cellList.add(cellModel);
            }
        }
        model.setCellList(cellList);

        return model;
    }

    public DicChkplatModel updateDicChkplat(DicChkplatModel model) {
        DicChkplat dicChkplat = dicChkplatMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, dicChkplat);
        int i = dicChkplatMapper.updateByPrimaryKey(dicChkplat);
        //工装表的工位数发生变化后,要同步更新工位表的 2022-10-18 guozhao(先不调整,设计有问题,后面整体改)
        return model;
    }

    public DicChkplatModel findDicChkplatById(int id) {
        DicChkplatModel model = new DicChkplatModel();
        DicChkplat dicChkplat = dicChkplatMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(dicChkplat, model);
        return model;
    }

    public DicChkplatModel findDicChkplatByPlatId(String platId) {
        DicChkplatModel model = new DicChkplatModel();
        DicChkplatExample example = new DicChkplatExample();
        DicChkplatExample.Criteria criteria = example.createCriteria();
        criteria.andPlatIdEqualTo(platId);
        List<DicChkplat> dicChkplats = dicChkplatMapper.selectByExample(example);
        if (!dicChkplats.isEmpty()) {
            BeanUtils.copyProperties(dicChkplats.get(0), model);
            return model;
        } else {
            return null;
        }
    }

    public int deleteDicChkplat(int id) {
        DicChkplat dicChkplat = dicChkplatMapper.selectByPrimaryKey(id);
        int i = dicChkplatMapper.deleteByPrimaryKey(id);
        dicChkplatCellService.deleteDicChkplatCellByPlanid(dicChkplat.getPlatId());
        return i;
    }
}
