package com.hxhq.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.StorageLocationJcgj;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.yq.StorageLocationForm;
import com.hxhq.business.form.yq.StorageLocationSearchForm;
import com.hxhq.business.service.IStorageLocationJcgjService;
import com.hxhq.business.service.IYqJcgjService;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.system.api.domain.SysDept;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysDeptService;
import com.hxhq.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StorageLocationMapper;
import com.hxhq.business.domain.StorageLocation;
import com.hxhq.business.service.IStorageLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 存储位置Service业务层处理
 *
 * @author hxhq
 * @date 2026-03-02
 */
@Service
public class StorageLocationServiceImpl extends ServiceImpl<StorageLocationMapper, StorageLocation> implements IStorageLocationService
{

    @Autowired
    private IStorageLocationJcgjService storageLocationJcgjService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询存储位置列表
     *
     * @param form 存储位置
     * @return 存储位置
     */
    @Override
    public List<StorageLocation> queryList(StorageLocationSearchForm form)
    {
        QueryWrapper<StorageLocation> queryWrapper = Wrappers.query();
        queryWrapper.eq("s.del_flag", 0);
        if(StringUtils.isNotEmpty(form.getName())) {
            queryWrapper.like("s.name", form.getName());
        }
        if(StringUtils.isNotEmpty(form.getLocation())) {
            queryWrapper.like("s.location", form.getLocation());
        }
        if(StringUtils.isNotEmpty(form.getShelfPlacement())) {
            queryWrapper.like("s.shelf_placement", form.getShelfPlacement());
        }
        if(form.getStatus() != null) {
            queryWrapper.eq("s.status", form.getStatus());
        }
        if(CollectionUtils.isNotEmpty(form.getDeptIdList())) {
            queryWrapper.in("s.dept_id", form.getDeptIdList());
        }
        return baseMapper.queryList(queryWrapper);
    }

    @Override
    public StorageLocation queryInfo(Long id) {

        return baseMapper.queryInfo(id);
    }

    @Override
    public void addStorageLocation(StorageLocationForm form) {
        sysUserService.checkPassword(form.getQmrId(), form.getQmrmm(), false);
        StorageLocation storageLocation = new StorageLocation();
        BeanUtils.copyProperties(form, storageLocation);
        save(storageLocation);
        storageLocationJcgjService.saveJcgj(storageLocation, JcgjlxEnum.bj.getValue(), "新增存储位置", "Add Storage Location", JcmcysEnum.blue.getValue(), null, null);
    }

    @Override
    public void updateStorageLocation(StorageLocationForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        StorageLocation storageLocation = this.queryInfo(form.getId());
        if(storageLocation == null) {
            throw new ServiceException("存储位置不存在或已删除");
        }
        if(!storageLocation.getStatus().equals(form.getStatus())) {
            if(storageLocation.getStatus() != null && storageLocation.getStatus() == 1) {
                storageLocation.setStatusInfo("禁用");
            } else {
                storageLocation.setStatusInfo("正常");
            }

            if(form.getStatus() != null && form.getStatus() == 1) {
                form.setStatusInfo("禁用");
            } else {
                form.setStatusInfo("正常");
            }
        }

        if(!storageLocation.getDeptId().equals(form.getDeptId())) {
            SysDept sysDept = sysDeptService.selectDeptById(form.getDeptId());
            form.setDeptName(sysDept.getDeptName());
        }


        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjects(storageLocation, form);
        if (fieldChanges.size() == 0) {
            throw new SecurityException("你没有修改任何内容");
        }
        BeanUtils.copyProperties(form, storageLocation);
        updateById(storageLocation);

        // 稽查轨迹
        List<StorageLocationJcgj> jcgjList = new ArrayList<>();
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            StorageLocationJcgj jcgj = new StorageLocationJcgj();
            jcgj.setStorageLocationId(storageLocation.getId());
            jcgj.setJcgjlx(JcgjlxEnum.bj.getValue());
            jcgj.setJcmc("编辑存储位置");
            jcgj.setJcmcEn("Edit Storage Location");
            jcgj.setJcmcys(JcmcysEnum.orange.getValue());
            jcgj.setJcnr(fieldChange.toString());
            jcgj.setJcnrEn(fieldChange.toEnString());
            jcgj.setQmrId(qmr.getUserId());
            jcgj.setQmrMc(qmr.getNickName());
            jcgj.setQmrMcEn(qmr.getUserName());
            jcgjList.add(jcgj);
        }
        storageLocationJcgjService.saveBatchWithLog(storageLocation, jcgjList);
    }

    @Override
    public void addBatch(List<StorageLocation> importDataList, StorageLocationForm form) {
        this.saveBatch(importDataList);
        for (StorageLocation storageLocation : importDataList) {
            storageLocationJcgjService.saveJcgj(storageLocation, JcgjlxEnum.bj.getValue(),
                    "新增存储位置", "Add Storage Location", JcmcysEnum.blue.getValue(), null, null);
        }
    }

}
