package com.foreverwin.mesnac.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.constant.Constants;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.mapper.ToolMapper;
import com.foreverwin.mesnac.common.model.Tool;
import com.foreverwin.mesnac.common.service.ToolService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 工具表 服务实现类
 * </p>
 *
 * @author pavel.liu
 * @since 2021-07-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ToolServiceImpl extends ServiceImpl<ToolMapper, Tool> implements ToolService {


    @Autowired
    private ToolMapper toolMapper;
    @Autowired
    private ToolService toolService;

    @Override
    public IPage<Tool> selectPage(FrontPage<Tool> frontPage, Tool tool) {
        QueryWrapper<Tool> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(tool);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<Tool> selectList(Tool tool) {
        QueryWrapper<Tool> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(tool);
        return super.list(queryWrapper);
    }

    @Override
    public void saveOrUpdateOne(Tool tool) {
        String site = tool.getSite();
        String toolNo = tool.getTool();
        String handle = HandleEnum.TOOL.getHandle(site, toolNo);
        String user = CommonMethods.getUser();
        tool.setHandle(handle);
        Tool byId = toolMapper.selectById(handle);
        if (byId != null){
            tool.setStatus(Constants.TOOL_STATUS_Y);
            tool.setCreateUser(byId.getCreateUser());
            tool.setCreatedDateTime(byId.getCreatedDateTime());
            tool.setModifyUser(user);
            tool.setModifiedDateTime(LocalDateTime.now());
            toolMapper.updateById(tool);
        } else{
            tool.setStatus(Constants.TOOL_STATUS_Y);
            tool.setCreateUser(user);
            tool.setCreatedDateTime(LocalDateTime.now());
            toolMapper.insert(tool);
        }

    }

    @Override
    public Tool getOneToolByToolNo(Tool tool) {
        String site = CommonMethods.getSite();
        String handle = HandleEnum.TOOL.getHandle(site, tool.getTool());
        tool.setHandle(handle);
        List<Tool> tools = toolService.selectList(tool);
        if (tools.size() == 1){
            return tools.get(0);
        }
        throw BusinessException.build("查询失败！");
    }

    @Override
    public void deleteInspectionPlanByHandle(String tool) {
        String site = CommonMethods.getSite();
        String handle = HandleEnum.TOOL.getHandle(site, tool);
        String user = CommonMethods.getUser();
        Tool byId = toolMapper.selectById(handle);
        if (byId == null){
            throw BusinessException.build("刀具编号不存在！");
        }
        byId.setStatus("N");
        byId.setModifyUser(user);
        byId.setModifiedDateTime(LocalDateTime.now());
        toolMapper.updateById(byId);
    }

    @Override
    public String saveOrUpdateToolByImport(JSONObject json, String sqlType) {
        String user = CommonMethods.getUser();
        String site = json.getString("site").trim();
        String tool = json.getString("tool").trim();
        String handle = HandleEnum.TOOL.getHandle(site, tool).trim();
        String description = json.getString("description").trim();
        String specification = json.getString("specification").trim();
        String brand = json.getString("brand").trim();
        String remark = json.getString("remark").trim();
        if (tool == null || tool.equals("")){
            return "刀具编号不能为空";
            //throw BusinessException.build("刀具编号不能为空！");
        }
        Tool target = new Tool();
        target.setStatus(Constants.TOOL_STATUS_Y);
        target.setSite(site);
        switch (sqlType){
            case "insert":
                target.setTool(tool);
                target.setHandle(handle);
                target.setDescription(description);
                target.setSpecification(specification);
                target.setBrand(brand);
                target.setCreateUser(user);
                target.setCreatedDateTime(LocalDateTime.now());
                toolMapper.insert(target);
                return "新增一条数据成功";
            case "update":
                Tool byId = toolMapper.selectById(handle);
                if (byId == null){
                    //throw BusinessException.build("更新失败，刀具编号不存在！");
                    return  "更新失败，刀具编号不存在！";
                }
                if (description != ""){
                    byId.setDescription(description);
                }
                if (specification != ""){
                    byId.setSpecification(specification);
                }
                if (brand != ""){
                    byId.setBrand(brand);
                }
                if (remark != ""){
                    byId.setRemark(remark);
                }
                toolMapper.updateById(byId);
                return "更新一条数据成功";
            case "both":
                Tool selId = toolMapper.selectById(handle);
                if (selId == null){
                    target.setTool(tool);
                    target.setHandle(handle);
                    target.setDescription(description);
                    target.setSpecification(specification);
                    target.setBrand(brand);
                    target.setCreateUser(user);
                    target.setCreatedDateTime(LocalDateTime.now());
                    toolMapper.insert(target);
                    return "新增一条数据成功";
                }
                if (description != ""){
                    selId.setDescription(description);
                }
                if (specification != ""){
                    selId.setSpecification(specification);
                }
                if (brand != ""){
                    selId.setBrand(brand);
                }
                if (remark != ""){
                    selId.setRemark(remark);
                }
                selId.setModifyUser(user);
                selId.setModifiedDateTime(LocalDateTime.now());
                toolMapper.updateById(selId);
                return "更新一条数据成功";
            default:
                throw BusinessException.build("更新/导入类型未找到！");
        }
    }



}