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.common.selectModel;
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.ResponseCodeMsg;
import com.shinsoft.sto.model.system.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 = ResultJSON.success(list);
        return resultJSON;
    }

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

    @Override
    public ResultJSON save(String userId,String belongId, String json) {
        Date date = new Date();
        boolean success;
        Customer customer = JSON.parseObject(json, new TypeReference<Customer>() {
        });
        if (StringUtils.isEmpty(customer.getId())) {
            //增加插入校验，相同客户名称的信息只能保存一条
            //增加限制的原因为：入库批量导入的时候使用到的客户名称进行查询赋值，如果出现两条一样的会出现错误
            customer.setCancelFlag("0");
            customer.setAddId(userId);
            customer.setAddTime(date);

            List<Customer> customers = customerMapper.selectByName(customer.getCustomerNm(), customer.getCustomerShortNm(), belongId);
            if (customers != null && !customers.isEmpty()) {
                return ResultJSON.error("客户名称或简称已存在，请勿重复添加");
            }
            success = customerMapper.insert(customer)> 0;
        } else {
            customer.setModifyId(userId);
            customer.setModifyTime(date);
            success = customerMapper.updateById(customer)> 0;
        }
        return success ? ResultJSON.success("新增成功") : ResultJSON.error("新增失败");
    }

    @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 = ResultJSON.success("");
        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 = ResultJSON.success("");
        return resultJSON;
    }

    @Override
    public ResultJSON querySelect(String belongId) {
        try {
            List<selectModel> list = customerMapper.selectCustomerOptions(belongId);
            resultJSON = ResultJSON.success(list);
            return resultJSON;
        } catch (Exception ex) {
            ex.printStackTrace();
            resultJSON = ResultJSON.error("查询失败");
            return resultJSON;
        }
    }
}
