package com.bonus.material.toolLedger.service.Impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.devchange.mapper.MaDevInfoMapper;
import com.bonus.material.device.domain.vo.DevInfoPropertyVo;
import com.bonus.material.device.mapper.DevMergeMapper;
import com.bonus.material.toolLedger.domain.ToolLedgerAllEntity;
import com.bonus.material.toolLedger.domain.ToolLedgerEntity;
import com.bonus.material.toolLedger.mapper.ToolLedgerMapper;
import com.bonus.material.toolLedger.service.ToolLedgerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.bonus.common.biz.constant.MaterialConstants.ADMIN_ID;
import static com.bonus.common.biz.constant.MaterialConstants.PROVINCE_COMPANY_DEPT_ID;

@Service
public class ToolLedgerServiceImpl implements ToolLedgerService {
    private static final Logger log = LoggerFactory.getLogger(ToolLedgerServiceImpl.class);
    @Resource
    private ToolLedgerMapper mapper;
    @Resource
    private DevMergeMapper devMergeMapper;
    @Autowired
    private MaDevInfoMapper maDevInfoMapper;

    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @Override
    public List<ToolLedgerAllEntity> list(ToolLedgerAllEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
//        if (userId != null && deptId != null
//                && !userId.equals(ADMIN_ID)
//                && !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
//            entity.setCompanyId(deptId);
//        }
        if(ADMIN_ID.equals(userId)){
            entity.setCompanyId(1L);
        }else{
            entity.setCompanyId(deptId);
        }
        return mapper.list(entity);
    }

    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @Override
    public List<ToolLedgerEntity> listCode(ToolLedgerEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
//        if (userId != null && deptId != null
//                && !userId.equals(ADMIN_ID)
//                && !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
//            entity.setCompanyId(deptId);
//        }
        if(ADMIN_ID.equals(userId)){
            entity.setCompanyId(1L);
        }else{
            entity.setCompanyId(deptId);
        }
        List<ToolLedgerEntity> toolLedgerEntities = mapper.listCode(entity);

        toolLedgerEntities.forEach(toolLedger -> {
            List<DevInfoPropertyVo> propertiiesList = mapper.getProperties(toolLedger.getId());
            if (propertiiesList != null && !propertiiesList.isEmpty()) {
                toolLedger.setPropertyVoList(propertiiesList);
            }

            toolLedger.setCertificates(maDevInfoMapper.getFileList(Math.toIntExact(toolLedger.getId() + 100000L), 2));
            toolLedger.setInspectionReports(maDevInfoMapper.getFileList(Math.toIntExact(toolLedger.getId() + 100000L), 3));
            toolLedger.setPurchaseInvoices(maDevInfoMapper.getFileList(Math.toIntExact(toolLedger.getId() + 100000L), 4));
        });

        return toolLedgerEntities;
    }

    /**
     * @param entity
     * @return
     */
    @Override
    public AjaxResult updateById(ToolLedgerEntity entity) {
        try {
            Integer num = mapper.updateById(entity);

            if (!entity.getPropertyVoList().isEmpty()) {
                mapper.delProperties(entity.getId());
                mapper.insertDevInfoProperties(entity.getPropertyVoList(), entity.getId());
                maDevInfoMapper.delFileList(Math.toIntExact(entity.getId() + 100000L), 2);
                entity.getCertificates().forEach(item -> {
                    // 这里编写对每个 image 的处理逻辑，比如打印、处理等
                    item.setFileType(2);
                    item.setMaId(Math.toIntExact(entity.getId() + 100000L));
                    item.setCreator(Math.toIntExact(SecurityUtils.getLoginUser().getUserid()));
                    devMergeMapper.interFile(item);
                });
                maDevInfoMapper.delFileList(Math.toIntExact(entity.getId() + 100000L), 3);

                entity.getInspectionReports().forEach(item -> {
                    // 这里编写对每个 image 的处理逻辑，比如打印、处理等
                    item.setFileType(3);
                    item.setMaId(Math.toIntExact(entity.getId() + 100000L));
                    item.setCreator(Math.toIntExact(SecurityUtils.getLoginUser().getUserid()));
                    devMergeMapper.interFile(item);
                });

                maDevInfoMapper.delFileList(Math.toIntExact(entity.getId() + 100000L), 4);

                entity.getPurchaseInvoices().forEach(item -> {
                    // 这里编写对每个 image 的处理逻辑，比如打印、处理等
                    item.setFileType(4);
                    item.setMaId(Math.toIntExact(entity.getId() + 100000L));
                    item.setCreator(Math.toIntExact(SecurityUtils.getLoginUser().getUserid()));
                    devMergeMapper.interFile(item);
                });

            }
            return num > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * @param entity
     * @return
     */
    @Override
    public List<ToolLedgerEntity> getToolByTypeId(ToolLedgerEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
        if (!userId.equals(ADMIN_ID) || !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
            entity.setCompanyId(deptId);
        }
        return mapper.getToolByTypeId(entity);
    }

    /**
     * @param entity
     * @return
     */
    @Override
    public List<ToolLedgerEntity> getToolByPro(ToolLedgerEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
        if (!userId.equals(ADMIN_ID) || !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
            entity.setCompanyId(deptId);
        }
        return mapper.getToolByPro(entity);
    }

    @Override
    public List<ToolLedgerEntity> getToolByOrder(ToolLedgerEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
        if (!userId.equals(ADMIN_ID) || !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
            entity.setCompanyId(deptId);
        }
        return mapper.getToolByOrder(entity);
    }
}
