package com.shinsoft.sto.service.impl.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinsoft.sto.mapper.main.CustomerMapper;
import com.shinsoft.sto.model.main.Customer;
import com.shinsoft.sto.service.main.CustomerService;
import com.shinsoft.tools.JSONTools;
import com.shinsoft.tools.model.common.ResultJSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库客户 服务实现类
 * </p>
 *
 * @author system
 * @since 2024-10-15
 */
@Service
public class CustomerServiceImp extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    ResultJSON resultJSON;

    @Autowired
    CustomerMapper customerMapper;

    @Override
    public ResultJSON query(int page, int rows, String params, Map<String, Object> map) {
        Page<Customer> customerPage = new Page<>(page, rows);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cancel_flag", "0");
        queryWrapper.orderByDesc("add_time");
        
        // 解析查询参数
        if (!ObjectUtils.isEmpty(params)) {
            queryWrapper.like("customer_nm", params);
//            JSONObject jsonObject = JSON.parseObject(params);
//            if (!ObjectUtils.isEmpty(jsonObject.get("customerNm"))) {

//            }
//            if (!ObjectUtils.isEmpty(jsonObject.get("customerShortNm"))) {
//                queryWrapper.like("customer_short_nm", jsonObject.getString("customerShortNm"));
//            }
//            if (!ObjectUtils.isEmpty(jsonObject.get("contactPerson"))) {
//                queryWrapper.like("contact_person", jsonObject.getString("contactPerson"));
//            }
        }
        
        Page<Customer> list = customerMapper.selectPage(customerPage, queryWrapper);
        resultJSON = JSONTools.toResultJSON(list);
        return resultJSON;
    }

    @Override
    public ResultJSON queryByPK(String id) {
        Customer customer = customerMapper.selectById(id);
        resultJSON = JSONTools.toResultJSON(customer);
        return resultJSON;
    }

    @Override
    public ResultJSON save(String userId, String json) {
        Date date = new Date();
        Customer customer = JSON.parseObject(json, new TypeReference<Customer>() {
        });
        if (StringUtils.isEmpty(customer.getId())) {
            customer.setCancelFlag("0");
            customer.setAddId(userId);
            customer.setAddTime(date);
            customerMapper.insert(customer);
        } else {
            customer.setModifyId(userId);
            customer.setModifyTime(date);
            customerMapper.updateById(customer);
        }

        resultJSON = JSONTools.toResultJSON(customer);
        return resultJSON;
    }

    @Override
    public ResultJSON remove(String userId, String id) {
        Date date = new Date();
        Customer customer = customerMapper.selectById(id);
        customer.setCancelFlag("1");
        customer.setModifyId(userId);
        customer.setModifyTime(date);
        customerMapper.updateById(customer);
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }

    @Override
    public ResultJSON removeBatch(String userId, String ids) {
        Date date = new Date();
        List<String> list = JSON.parseObject(ids, new TypeReference<List<String>>() {
        });
        Customer customer;
        for (String id : list) {
            customer = customerMapper.selectById(id);
            customer.setCancelFlag("1");
            customer.setModifyId(userId);
            customer.setModifyTime(date);
            customerMapper.updateById(customer);
        }
        resultJSON = JSONTools.toResultJSON("");
        return resultJSON;
    }
}
